Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created an event handler for ItemAdded so that when an item is added on the site, my code checks to see if it's a folder. If it is, it should change the content type of that folder to a custom content type.

The event isn't firing, so I was wondering if I'm doing it the right way and/or in the right place. Here is my code:

public class ItemAddedHandler : SPItemEventReceiver
{
    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);

        SPListItem listItem = properties.ListItem;
        SPList list = properties.ListItem.ParentList;
        SPContentType contentType;

        if (listItem.FileSystemObjectType == SPFileSystemObjectType.Folder &&
           (list.Title == "Apps" || list.Title == "Data" || list.Title == "Public"))
        {
            contentType = list.ContentTypes["Custom Folder"];

            listItem["Content Type"] = contentType.Name;
            listItem["Content Type ID"] = contentType.Id.ToString();
            listItem.SystemUpdate();
        }
    }
}

Please let me know if I'm doing anything wrong to accomplish this task. Thanks very much in advance.

As requested, the feature was adding with the following method:

  1. Signed and compiled above code and copied DLL to server
  2. Copied DLL to GAC
  3. Created folder: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\SPEventHandler
  4. Created Feature.xml using GUID generated with Visual Studio:
<?xml version="1.0" encoding="utf-8" ?>
 <Feature Scope="Web" Title="Added Event Handler" 
   Id="{27C2FDFF-ADA0-4984-955C-6448E182FA88}" 
   xmlns="http://schemas.microsoft.com/sharepoint/">
   <ElementManifests>
     <ElementManifest Location="Elements.xml"/>
   </ElementManifests>
 </Feature>
  1. Created Elements.xml using PublicKeyToken of DLL in GAC and ListTemplateID for working with a document library (101):
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
    <Receiver>
      <Name>AddedEventHandler</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>SPEventHandler, Version=1.0.0.0, Culture=neutral, 
        PublicKeyToken=f2e7de6c4a924a03</Assembly>
      <Class>SPEventHandler.ItemAddedHandler</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>
</Elements>
  1. Ran the following command on the server: stsadm -o installfeature -filename SPEventHandler\Feature.xml
  2. On the site (SP 2007), Site Actions -> Site Settings -> Modify All Site Settings -> Site features and activated "Added Event Handler"

After these steps, creating a new item in a list on this site does not seem to do anything, including writing to the event log, so I'm wondering if it is even getting run at all.

Thanks

share|improve this question

2 Answers 2

What about changing:

listItem["Content Type"] = contentType.Name;
listItem["Content Type ID"] = contentType.Id.ToString();

to:

listItem["ContentTypeId"] = contentType.Id;
share|improve this answer
    
That didn't work. –  Music Magi Jun 2 '11 at 13:21
1  
MBSurf's suggestion should work and is what I've used. –  Wictor Wilen MCA MCM MVP Jun 2 '11 at 13:38
1  
How did you add the event handler to the list/content type? Can you edit your question and add the feature's CAML and any feature activation code? –  MBSurf Jun 2 '11 at 16:57
1  
Have you checked the ULS Logs? Typically, if SharePoint can't instantiate the class, it will let you know in there right after you create the item. –  MBSurf Jun 2 '11 at 18:19
1  
The other thing I noticed is that you have { } brackets around your feature Id. Typically for feature Ids these are not recommended. Note: this is sort of a long shot since it seems to be seeing your feature after you run your install command. –  MBSurf Jun 2 '11 at 18:22
up vote 1 down vote accepted

I figured it out. My custom content type needed to be enabled for the document library before an item could be given that content type. Thanks everyone for your responses.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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