2

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();
            }   
        }
2
  • Please add the declarative code you are using Commented Oct 27, 2011 at 16:42
  • Hi, I added the code
    – Patrick
    Commented Oct 28, 2011 at 10:46

1 Answer 1

1

If you go to the ListTemplateId enumeration, it specifies that the 106 would be for Calendars. So according to this, I would take the 101.
Further explanations on the registration on events can be found on msdn: Event Registrations

6
  • Hi,Thanks for your answer. It was my mistake. I used 101. For testing I also tried differend ID's.
    – Patrick
    Commented Oct 28, 2011 at 8:38
  • My bad. It should be 101. I did some tests with other id's . However the problem still exists. Then even if I use 106, it is also fired when creating usergroups etc.. so in this case not only for calendars.
    – Patrick
    Commented Oct 28, 2011 at 8:39
  • Could you please show the whole event receiver XML definition to see if there's something wrong...
    – Matthias
    Commented Oct 28, 2011 at 8:48
  • Elements.xml code added to my original post
    – Patrick
    Commented Oct 28, 2011 at 9:32
  • 1
    I found something out today. It seems that when a features as a scope"site" it ignores the templateID and just fires for every list. What is the best way to deal with this ? I need it to fire for every document library in a lot of sites (within the sitecollection) so scope"web"is not an option for me I think.
    – Patrick
    Commented Oct 28, 2011 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.