How to attach Event Receiver to SPList programmatically? - c#

I'm working on a project i SharePoint 2010 where I have several under sites. each under site contain a list with news and I want to attach an Event Receiver to those lists.
The under sites and lists are created programmatically but I cannot attach the Event Receiver I have in my VS2010 Solution.
I've tried with this:
SPList list = new SPSite(siteURL).OpenWeb().Lists[listName];
SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;
SPEventReceiverDefinition eventReceiver = eventReceivers.Add();
eventReceiver.Name = receiverName;
eventReceiver.Synchronization = SPEventReceiverSynchronization.Synchronous;
eventReceiver.Type = SPEventReceiverType.ItemAdded;
eventReceiver.SequenceNumber = sequenceNumber;
eventReceiver.Assembly = assemblyFullName;
eventReceiver.Class = assemblyClassName;
eventReceiver.Data = receiverData;
eventReceiver.Update();
But it does not work.
The error message is "Could not load file or assembly 'Projekt_Test1\, \, Version\=1.0.1777.23493\, Culture\=neutral\, PublicKeyToken\=49c7547d535382ab' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)"
Thanks for help.

I end up creating a List Extension method for this:
public static void EnsureEventReceiver(this SPList list,IEnumerable<SPEventReceiverType> receiverTypes, Type eventHander, SPEventReceiverSynchronization synchronization, int sequenceNumber)
{
foreach (SPEventReceiverType spEventReceiverType in receiverTypes)
{
string name = list.Title + spEventReceiverType.ToString();
if (list.EventReceivers.Cast<SPEventReceiverDefinition>().All(i => i.Name != name))
{
SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();
eventReceiver.Name = name;
eventReceiver.Type = spEventReceiverType;
eventReceiver.Assembly = eventHander.Assembly.FullName;
eventReceiver.Class = eventHander.FullName;
eventReceiver.SequenceNumber = sequenceNumber;
eventReceiver.Synchronization = synchronization;
eventReceiver.Update();
}
}
}
Caveats, Limitations of this method:
Only one event per List, since this is good enough for me, if you need more then you need to pass the name as a parameter
Event handler methods are in the same class
You can use it like this:
list.EnsureEventReceiver(
new[] { SPEventReceiverType.ItemAdded, SPEventReceiverType.ItemUpdated },
typeof(NewsItemsHandler),
SPEventReceiverSynchronization.Synchronous,
10000);

I have never succeeded with this version of eventReceivers.Add() you are using.
Here is a powershell framgent I'm using, it would be very similar in C#
$ev = $currentList.EventReceivers.Add([Microsoft.SharePoint.SPEventReceiverType]::ItemAdded, $assemblyName, $className);

Couple of things to look at:
Your assembly version is listed as 1.0.1777.23493. That looks like it is being auto-incremented. You will want to set a fixed assembly version or it will update with every build, breaking your code.
You're setting eventReceiver.Synchronization = SPEventReceiverSynchronization.Synchronous, but the ItemAdded is an asynchronous event.
Make sure that your event receiver assembly has been deployed to the GAC on the SharePoint server, and that you have recycled the SharePoint application pools in IIS before you run your code.

Related

System.Diagnostics.EventLog doesn't contain correct message

In some cases, when retrieving event logs from System.Diagnostics.EventLog, message like this
The description for Event ID '10016' in Source 'DCOM' cannot be found...
is returned. I found out that this response is also returned by Get-EventLog command in Powershell.
The actual message should look like this:
The application-specific permission settings do not grant Local Activation permission...
and is returned by Get-WinEvent command.
Is there any way to retrieve the second message in .Net Framework project? (without calling an independent Powershell script)?
UPDATE
I implemented the suggested solution, but now I stumbled on a different problem - how can I retrieve Audit Success and Audit Failure information? The EventLogEntry had an enum that contained them, but EventRecord doesn't
Update 2
I found a way to deal with Audits. EventRecord has a Keywords property, I compared it to StandardEventKeywords enum
As mentioned in the comments, Get-WinEvent uses the EventLogReader class to enumerate the events queried, and then calls EventRecord.FormatDescription() on each resulting record to render the localized message.
Here's a sample console application to fetch and print the rendered message of each of the first 10 Warning (Level=3) events in the Application log:
using System;
using System.Diagnostics.Eventing.Reader;
class Program
{
static void Main(string[] args)
{
// construct an EventLogQuery object from a log path + xpath query
var xpath = "*[System[Level=3]]";
var query = new EventLogQuery("Application", PathType.LogName, xpath);
// instantiate an EventLogReader over the query
var reader = new EventLogReader(query);
// read the events one by one
var counter = 0;
EventRecord record = null;
while ((record = reader.ReadEvent()) is EventRecord && ++counter <= 10)
{
// call FormatDescription() to render the message in accordance with your computers locale settings
var renderedMessage = record.FormatDescription();
Console.WriteLine(renderedMessage);
}
}
}
Beware that it's entirely possible for FormatDescription() to return an empty string - this will occur when the event logging provider didn't provide a message template for the given event id.
Thank you Mathias R. Jessen. Posting your suggestion as an answer to help other community members.
You can try and mimic what Get-WinEvent does: enumerate events with an EventLogReader, then call FormatDescription() on the resulting events to render the message
You can refer to EventLogReader Class and EventRecord.FormatDescription Method
Thanks to Mathias R. Jensen, I created something similar using XML query
static void Main(string[] args)
{
string logName = "System";
string sourceName = "DCOM";
string query = $#"<QueryList>
<Query Id=""0"" Path=""{logName}"">
<Select Path=""{logName}"">
*[System[Provider[#EventSourceName=""{sourceName}""]]]
and
*[System[TimeCreated[#SystemTime>=""{DateTime.UtcNow.AddMinutes(-10).ToString("O")}""]]]
</Select>
</Query>
</QueryList>";
EventLogQuery elq = new EventLogQuery("System", PathType.LogName, query);
EventLogReader elr = new EventLogReader(elq);
EventRecord entry;
while ((entry = elr.ReadEvent()) != null)
{
Console.WriteLine(entry.FormatDescription());
}
Console.ReadLine();
}

Need a Way to Search through Event Logs by RecordID

I am trying to search through a folder with Event Logs in them, eventpath has the path of the specific Event Log I want to access. I want to use a specified RecordID to find it's correlated FormatDescription and display it in a MessageBox. I want to be able to use the eventpath to access each Event Log since I am using 6 separate .evtx files and need to use this method on all of them.
I found this solution, but I get an error when I'm trying to Query. I've tried to find a fix, but it seems as if it's just not going to work for what I need. I commented where exactly it is occurring in the code.
This is the exception: System.Diagnostics.Eventing.Reader.EventLogException: The specified path is invalid.
I can't find a fix for this code, but if anyone knows a fix or another way to approach searching through Event Logs by RecordID and giving the corresponding FormatDescription, it would be greatly appreciated.
I am using C# in Windows Presentation Foundation.
public void getDesc(string recordid)
{
string eventpath = getEventPath();
//takes off the .evtx of the path
string result = eventpath.Substring(0, eventpath.Length - 5);
//result1 is going to be similar to this:
//C:\Users\MyName\AppData\Local\Temp\randomTempDirectory\additional_files\DiagnosticInfo\WindowsEventLogs\Application
string sQuery = "*[System/EventRecordID=" + recordid + "]";
var elQuery = new EventLogQuery(result, PathType.LogName, sQuery);
//this is where it errors out
//error: Specified Channel Path is invalid
using (var elReader = new System.Diagnostics.Eventing.Reader.EventLogReader(elQuery))
{
List<EventRecord> eventList = new List<EventRecord>();
EventRecord eventInstance = elReader.ReadEvent();
try
{
while ((eventInstance = elReader.ReadEvent()) != null)
{
//Access event properties here:
string formatDescription = eventInstance.FormatDescription();
MessageBox.Show(formatDescription);
}
}
finally
{
if (eventInstance != null)
eventInstance.Dispose();
}
}
}

How to get Workspace object in new TeamFoundation 2013 Templates

In new version of TeamFoundation 2013 default build templates, the Workspace variable is missing. It is needed as intput parameter for few key activities like ConvertWorkspaceItem. How do I get current workspace for TfvcTemplate.12.xaml templates? I've tried to use this msdn thread but it's not working for me (returns null workspace name). Any suggestions?
There's a new activity in 2013 called GetLocalPath that replaces ConvertWorkspaceItem.
The activity is under the Microsoft.TeamFoundation.Build.Activities.Core namespace in the Microsoft.TeamFoundation.Build.Activities assembly.
It uses the LocalPathProvider class that aggregates all workspaces used in the build and exposes path translation for all of them in one place. This basically removes the dependency of knowing the workspace in order to translate server paths to local paths and allows you to use as many workspaces as you want without worrying about breaking something down the line.
When MS takes something away, it's usually for a good reason. "hacking" is really not necessary.
I went with a hack using internal classes from Microsoft.TeamFoundation.Build.Activities.dll (used by microsoft to create workspace name). You need to create custom activity with following code:
public sealed class GetDefaultWorkspace : BaseActivity<Workspace>
{
public override Activity CreateBody()
{
var type = typeof(TfGetSources).Assembly.GetType("Microsoft.TeamFoundation.Build.Activities.TeamFoundation.TfGetSources+GetDefaultWorkspaceName");
var activity = (CodeActivity<string>)Activator.CreateInstance(type);
var sequence = new Sequence();
var workspaceName = new Variable<string>();
sequence.Variables.Add(workspaceName);
sequence.Activities.Add(activity);
activity.Result = (OutArgument<string>) workspaceName;
sequence.Activities.Add(new GetWorkspace
{
Name = workspaceName,
Result = new LambdaReference<Workspace>(ctx => Result.Get(ctx))
});
return sequence;
}
}
This answer might work better for some people. ghord's answer works well, and passes the Workspace back where it can be used in the XAML. However, for my purposes I only want the workspace in my custom TFS activities, so I ended up with this alternative...
public sealed class CustomActivity : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
// get workspace
var buildDetail = context.GetExtension<IBuildDetail>();
var buildAgent = context.GetExtension<IBuildAgent>();
var buildDirectory = buildAgent.GetExpandedBuildDirectory(buildDetail.BuildDefinition);
var workspacePath = Path.Combine(buildDirectory, "src");
var wsInfo = Workstation.Current.GetLocalWorkspaceInfo(workspacePath);
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(wsInfo.ServerUri);
tfs.Connect(ConnectOptions.None);
var vcs = tfs.GetService<VersionControlServer>();
// finally can get to the workspace here
var workspace = vcs.GetWorkspace(workspacePath);
}
}
Using this method, I don't have to have an activity that just returns the workspace, and then have to pass the workspace into other TFS activities. I just get the workspace from within my own activity while it runs.
I believe the method employed here will use the already downloaded workspace. Keep in mind, that this approach will only work within the scope of "Run on agent" sequence after "Initialize Environment" and before ResetEnvironment within the finally statement of Try Compile, Test, Publish. Else, the workflow will have no knowledge of a sources directory.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/420ba073-bdf5-4ab4-88da-c84561d1a1ba/creating-dynamic-working-folder-in-tfs2013-defaulttemplate?forum=tfsbuild

C# - Loading dlls and creating instances

I have a situation and I need to know how to deal with it in the best way.
I have an application (MVC3) and I have couple of integrations for it. I have an interface "IntegrationInterface" and every integration implements it.
I want to load the dlls of the integrations, create a list of them, and run a loop that runs a method for every integration in the list.
For example - let's say I have integrations for facebook, myspace and twitter (for my appliction), and every time the user post a message in my application I want to post a message on his\her facebook, myspace and twitter.
I don't want that the code will know which integrations I have, so if tomorrow I'll create a new integration for google+, I'll just need to add a new DLL without changing the code of my application.
How can I do that?
First, you'll have to find all relevant dlls and classes:
loadedIntegrations.Clear();
if (!Directory.Exists(path))
return;
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles("*.dll");
foreach (var file in files)
{
Assembly newAssembly = Assembly.LoadFile(file.FullName);
Type[] types = newAssembly.GetExportedTypes();
foreach (var type in types)
{
//If Type is a class and implements the IntegrationInterface interface
if (type.IsClass && (type.GetInterface(typeof(IntegrationInterface).FullName) != null))
loadedIntegrations.Add(type);
}
}
loadedIntegrations is of type List<Type>. Then you can instantiate each integration and call its methods:
foreach(var integrationType in loadedIntegrations)
{
var ctor = integrationType.GetConstructor(new Type[] { });
var integration = ctor.Invoke(new object[] { }) as IntegrationInterface;
//call methods on integration
}
I am doing something similar to what you described in an import utility that wrote. My issue was that I didn't want to load ALL the assemblies. I only wanted to load assemblies that contained types that were requested.
To accomplish this I've used the AppDomain.CurrentDomain.AssemblyResolve event handler.
This event handler is raised just before the AppDomain throw an exception notifying that an assembly is not found. I execute similar code to what Nico suggested in that handler and return the requested assembly.
NOTE: I have a sub-directory called 'Tasks' (think Import Tasks) where I store all of my assemblies I want to load at runtime.
Here is the code:
var tasks = GetTasks();
var basedir = AppDomain.CurrentDomain.BaseDirectory; // Get AppDomain Path
var tasksPath = Path.Combine(basedir, "Tasks"); // append 'Tasks' subdir
// NOTE: Cannot be factored, relies on 'tasksPath' variable (above).
AppDomain.CurrentDomain.AssemblyResolve += (s, e) => // defined 'AssemblyResolve' handler
{
var assemblyname = e.Name + ".dll"; // append DLL to assembly prefix
// *expected* assembly path
var assemblyPath = Path.Combine(tasksPath, assemblyname); // create full path to assembly
if (File.Exists(assemblyPath)) return Assembly.LoadFile(assemblyPath); // Load Assembly as file.
return null; // return Null if assembly was not found. (throws Exception)
};
foreach (var task in tasks.OrderBy(q => q.ExecutionOrder)) // enumerate Tasks by ExecutionOrder
{
Type importTaskType = Type.GetType(task.TaskType); // load task Type (may cause AssemblyResolve event to fire)
if (importTaskType == null)
{
log.Warning("Task Assembly not found");
continue;
}
ConstructorInfo ctorInfo = importTaskType.GetConstructor(Type.EmptyTypes); // get constructor info
IImportTask taskInstance = (IImportTask)ctorInfo.Invoke(new object[0]); // invoke constructor and cast as IImportTask
taskInstances.Add(taskInstance);
}
// rest of import logic omitted...
If u know MEF (Managed extensibility framework) this might help you I personally use it but have to say that using MEF with MVC is not trivial i think for more information please visit
http://msdn.microsoft.com/en-us/library/dd460648.aspx

Unexplained delay when assigning a string to an SPListItem field

I am using the SharePoint Object Model via a console app on the same server as the SharePoint installation, and using the following code:
SPSite MySite = new SPSite("http://server/");
SPWeb MyWeb = MySite.OpenWeb();
MyWeb.AllowUnsafeUpdates = true;
SPList MyList = MyWeb.Lists["Test"];
const string EmptyQuery = "0";
SPQuery q = new SPQuery { Query = EmptyQuery };
String Source = "Test String";
for( int i = 1; i < 1000; i++)
{
Console.WriteLine("Creating new item");
SPListItem MyItem = MyList.GetItems(q).Add();
Console.WriteLine("Created new item");
Console.WriteLine("Assigning Title Value");
MyItem["Title"] = Source.ToString();
Console.WriteLine("Assigned Title Value");
MyItem.Update();
}
I am getting a several second pause between "Assigning Title Value" and "Assigned Title Value".
When I deploy the code as a Web Part, its instantaneous, the delay only seems to be when the code is deployed as a console application.
Edit: More information! When I have more than one field being assigned, its always the first field that is slow, any subsequent assignments are as fast as expected. If I switch the order of fields around, it has no effect on the delay - the first field is always slow.
Any thoughts?
It looks like this is because you're not accessing any fields before using the setter. If you read at least one field first, you'd probably see a slight delay there, and no delay on the setter, because under the hood SetValue() calls EnsureFieldCollection(), which - if the fields for the list item haven't already been populated - has to check back to the SPList's fields collection.
Also, this is not in your code snippet, but make sure you are disposing of your SPWeb and SPSite objects when you're done. A good pattern is to use using:
using(SPSite site = new SPSite("url"))
{
using(SPWeb web = site.OpenWeb())
{
//do stuff
}
}
First of all I would suggest making use of
using (SPSite MySite = new SPSite("http://server/"))
{
using (SPWeb MyWeb = MySite.OpenWeb())
{
//do your stuff here
}
}
You can check the EnsureFieldCollection by reading all the fields of the item before updating the field. If after that your first field is updated instantly you can be pretty sure that that is the reason.
It may be a difference between Release and Debug builds, or the fact it has the debugger attatched. Try changing to a Release build, and if that doesn't work try running it without the debugger (Ctrl+F5 in Visual Studio)

Categories

Resources