I have created an event receiver with the "documents" as templateid (101).
<Receivers ListTemplateId ="101">
When I upload a document, the event gets fired. So no problem here. However the event also fires when I create a new usergroup, a new site etc..
I only want it to fire when a user uploads a document. What am I doing wrong, and how can I make sure that it only triggers for a 'real document (all filetypes)
Background info : The event receiver sets userrights on a document
Elements.XML
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId ="101">
<Receiver>
<Name>SetSecurityOnItemItemAdded</Name>
<Type>ItemAdded</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>MSH_SetSecurityOnItem.SetSecurityOnItem.SetSecurityOnItem</Class>
<SequenceNumber>1000</SequenceNumber>
</Receiver>
<Receiver>
<Name>SetSecurityOnItemItemUpdated</Name>
<Type>ItemUpdated</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>MSH_SetSecurityOnItem.SetSecurityOnItem.SetSecurityOnItem</Class>
<SequenceNumber>1000</SequenceNumber>
</Receiver>
</Receivers>
</Elements>
FEATURE.XML
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Title="MSH_SetSecurityOnItem Feature" Description="MSH_SetSecurityOnItem Feature" Id="52d11a53-c336-4446-abc0-a38d57b18704" Scope="Site">
<Properties>
<Property Key="ConfigFile" Value="SecurityConfig.xml" />
<Property Key="ConfigSchemaFile" Value="SecurityConfig.xsd" />
</Properties>
<ElementManifests>
<ElementManifest Location="SetSecurityOnItem\Elements.xml" />
<ElementFile Location="SetSecurityOnItem\SecurityConfig.xml" />
<ElementFile Location="SetSecurityOnItem\SecurityConfig.xsd" />
</ElementManifests>
</Feature>
ITEMUPDATED
public override void ItemUpdated(SPItemEventProperties properties)
{
if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null)
{
//This is when the update event is triggered by check-in.
}
else //This is triggered by events other than check-in action.
{
//Get configuration from feature
Guid featureGuid = new Guid("52d11a53-c336-4446-abc0-a38d57b18704");
SPWeb web = properties.OpenWeb();
SPSite site = properties.OpenSite();
SPFeature feature = site.Features[featureGuid]; //web.Features[featureGuid];
string ConfigFile = @"SetSecurityOnItem\" + feature.Properties["ConfigFile"].Value;
string SchemaFile = @"SetSecurityOnItem\" + feature.Properties["ConfigSchemaFile"].Value;
string ConfigFilePath = Path.Combine(feature.Definition.RootDirectory, ConfigFile);
string ConfigSchemaFilePath = Path.Combine(feature.Definition.RootDirectory, SchemaFile);
Trace.WriteLine("Current sitename is : " + web.Title);
Trace.WriteLine(DateTime.Now.ToString("dd/MM/yy hh:mm:ss tt") + " " + properties.ListItem.Name + " started Event Handler ItemUpdated");
//make sure that the config file exists
Trace.WriteLine(ConfigFilePath.ToString());
if (File.Exists(ConfigFilePath))
{
Trace.WriteLine(DateTime.Now.ToString("dd/MM/yy hh:mm:ss tt") + " " + "Config file exists");
// create the datatable
dt = new DataSet();
//load the configuration into an XMLReader
using (XmlReader rdr = XmlReader.Create(File.OpenRead(ConfigFilePath)))
{
dt.ReadXmlSchema(ConfigSchemaFilePath);
dt.ReadXml(rdr);
Trace.WriteLine(string.Format(DateTime.Now.ToString("dd/MM/yy hh:mm:ss tt") + " " + "Security config file contains {0} rows", dt.Tables[0].Rows.Count.ToString()));
Trace.WriteLine(string.Format(DateTime.Now.ToString("dd/MM/yy hh:mm:ss tt") + " " + "Security config file contains {0} columns", dt.Tables[0].Columns.Count.ToString()));
}
//pass the table to the SetSecurty method to do the real work
Trace.WriteLine(string.Format(DateTime.Now.ToString("dd/MM/yy hh:mm:ss tt") + " " + "The chosen security class for this item = {0}", properties.ListItem["Machtigingen"].ToString().Substring(1, 1)));
// this is were the magic starts
SetSecurityStores(properties, dt, properties.ListItem["Machtigingen"].ToString().Substring(1, 1));
SetSecurityHolding(properties, properties.ListItem["Machtigingen Holding"].ToString().Substring(1, 1));
}
else
{
Trace.WriteLine(DateTime.Now.ToString(DateTime.Now.ToString("dd/MM/yy hh:mm:ss tt") + " " + "dd/MM/yy hh:mm:ss tt") + " " + "Config file DOES NOT exist");
//TO DO Error message
}
base.ItemUpdated(properties);
web.Dispose();
}
}