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:
- Signed and compiled above code and copied DLL to server
- Copied DLL to GAC
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>
5.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</开发者_JAVA技巧SequenceNumber>
<Assembly>SPEventHandler, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=f2e7de6c4a924a03</Assembly>
<Class>SPEventHandler.ItemAddedHandler</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>
- Ran the following command on the server:
stsadm -o installfeature -filename SPEventHandler\Feature.xml
- 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
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.
精彩评论