Showing posts with label Content Type. Show all posts
Showing posts with label Content Type. Show all posts

Thursday, August 28, 2008

Attaching SPItemEventReceiver with a List Type

In my last post I have talked about writing an SPItemEventReceiver for fetching a workflow instance out of a Task item in a Task List type. Next step that comes to our mind is how we can attach this event receiver with a List. There are two ways in which you can attach your event receiver to a list type. One is through a feature: in elements.xml file you can write CAML and have Receiver attributes. another way is by using object model. I will explain you both the ways:

1) Feature way:

Create a feature.xml in the normal way as you do for any other feature. Here is the example:



"<"!--Created by Kunal Kochhar at 18/08/2008 10:21:18 AM--">"
"<"Feature Id="66D0E36C-4D48-46D0-90E9-7BFGHFBE4E3C" Title="Item receiver attacher" Description="This feature will attach event handler to a task item generated by Disposition workflow" Version="1.0.0.0" Scope="Web" Hidden="false" ImageUrl="CustomBranding.gif" xmlns="http://schemas.microsoft.com/sharepoint/"">"
"<"ElementManifests">"
"<"ElementManifest Location="elements.xml" /">"
"<"/ElementManifests">"
"<"/Feature">"



Now write the elements.xml file. This file will have Receivers tag. Read here more about Receivers element:

http://msdn.microsoft.com/en-us/library/ms431081.aspx

Let me show you the elements file:

'<'elements xmlns="http://schemas.microsoft.com/sharepoint/"'>'
'<'Receivers ListTemplateId="107"'>'
'<'Receiver'>'
'<'Name'>'TaskAdded'<'/Name'>'
'<'Type'>'ItemAdded'<'/Type'>'
'<'SequenceNumber'>'10043'<'/SequenceNumber'>'
'<'Assembly'>'assemblyname, [full four part]'<'/Assembly'>'
'<'Class'>'namespacename.classname'<'/Class'>'
'<'Data'>''<'/Data'>'
'<'Filter'>''<'/Filter'>'
'<'/Receiver'>'
'<'/Receivers'>'
'<'/Elements'>'



As you can see I am saying ListTemplateId=107 in the Receivers element. This id is the List Template Id for Tasks list type. You can refer to more list template ids here: http://msdn.microsoft.com/en-us/library/ms431081.aspx
Once you deploy and activate this feature on web it will attach your required event handler with all the existing tasks lists or any new ones that you create.

2) Lets see the second approach which is through code. The SPWeb object exposes list templates and contenttypes collections. You can retreive the required list template type and content type and can add your eventreceiver assembly in their eventreceivers collection. Here is an example to add it to a content type:

SPWeb web = SPContext.Current.Web;
SPContentType ct = web.ContentTypes["Tasks"];
SPEventReceiverDefinition receiverDef = ct.EventReceivers.Add();
receiverDef.Name = "Custom_ItemAdded";
receiverDef.Type = SPEventReceiverType.ItemAdded;
receiverDef.SequenceNumber = 10012;
receiverDef.Assembly = "assembly, [full four part assembly details]";
receiverDef.Class = "namespacename.classname";
receiverDef.Update();

I hope this will help some of you. If you have any queries in relation to this then please feel free to write to me.

Tuesday, August 19, 2008

Event Handler Explorer Tool

I am sure lot of you must have had problems registering event handlers with list types or content types. There is a very good tool developed by Patrick Tisseghem called as
Event Handler Explorer. This tool allows you to browse and list instances and content types and to attach even handlers manually. This way you will be assured that the event receiver is attached properly.

Thought of sharing this great tool with every one :-)

http://www.u2u.info/SharePoint/U2U%20Community%20Tools/EventHandlerExplorer.zip

Cheers,
Kunal

Tuesday, July 1, 2008

Custom Site Column in an existing OOTB List or ContentType

Today I will explain you on how to add a custom site column to an existing Out of the box (OOTB) List type or a content type. There will be instances during your project life cycle when you will want to provision a column to a particular List type or content type, for example you want to add a new column "Expert Comments" to a Wiki library. And you also want to provision this column to any new Wiki library created hereafter in the existing site.

Now for adding a new site column to custom list, you can add it directly when provisioning custom List Definition you can learn this here. But if you want to add a site column to already provisioned list you need to do it through object model.

Firstly you need to find the system content type for a List. Every list in in SharePoint uses a content type, either system content type or custom content type. You can view the content type hierarchy here. And you can find an associated content type for a system List using following lines of code in a console app:

using (SPSite site = new SPSite("http://devmoss/)"))

{

using (SPWeb web =
site.OpenWeb())

{

//List Content types in a web
foreach (SPContentType ct in web.ContentTypes)
{
Console.WriteLine(ct.Name);
}
Console.WriteLine("-------------------------");

//List Content types in a List
SPList list = web.Lists["Posts"];
foreach (SPContentType ctl in list.ContentTypes)
{
Console.WriteLine(ctl.Name);
}
Console.ReadLine();
}



Now once you know the content type of a List in which you wish to add a
custom column, you can use it add a new SPFieldLink to that SPContentType
object. Below is the sample code:




using (SPSite site = new SPSite("http://devmoss001/)"))
{
using (SPWeb web = site.OpenWeb())
{

try
{
string newColumn= "New Column";

//Construct required content type objects
SPContentType wikiContentType = web.ContentTypes["Wiki Page"];

//Check if the site column already exists
if (!web.Fields.ContainsField(newColumn))
{
//If the site column doesn't exist then create a new column and assign it to custom columns group
popularity = web.Fields.Add(newColumn, SPFieldType.Number, true);
SPFieldNumber fieldNum = (SPFieldNumber)web.Fields[newColumn];
fieldNum.Group = "Custom Columns";
fieldNum.Update(true);
}

if (!string.IsNullOrEmpty(newColumn))
{
//Get the field reference of new site column
SPFieldLink fieldLink = new SPFieldLink(web.Fields[newColumn]);

if (fieldLink != null)
{
try
{


//Add the site column to wiki content type
wikiContentType.FieldLinks.Add(fieldLink);
wikiContentType.Update(true);

}
catch (SPContentTypeSealedException ex)
{
}
catch (SPContentTypeReadOnlyException ctEx)
{
}
}

}

}

catch (SPException ex)
{
//Write to Logger
}


}
}


I hope that will be of some help to you. If you have any questions related to this concept then feel free to write to me.
Dogpile
Powered By Blogger