Wednesday, November 21, 2007

Coding against the SharePoint Object Model (Some useful code snippets)

I thought it might be handy to write down some useful code snippets. These snippets are often used when you code against the SharePoint Object Model...

Delete all items in a document library:
----------------------------------------------------

using (SPSite sitecollection = new SPSite("http://..."))
{
using (SPWeb web = sitecollection.OpenWeb())
{
SPList myList = web.Lists["Document library"];
int amountOfFiles = myList.Items.Count;

for (int i = amountOfFiles - 1; i >= 0; i--)
{
//Delete Document Library item
web.AllowUnsafeUpdates = true;
myList.Items.Delete(i);
web.AllowUnsafeUpdates = false;
}
}
}

Uploading a file to a document library:
----------------------------------------------------

using (SPSite sitecollection = new SPSite("http://..."))
{
using (SPWeb web = sitecollection.OpenWeb())
{
SPFolder docFolder = web.GetFolder("document library");

FileStream fStream = File.OpenRead(urlOfDocumentOnServer);
Byte[] ContentsContents = new byte[Convert.ToInt32(fStream.Length)];
fStream.Read(Contents, 0, Convert.ToInt32(fStream.Length)); }

web.AllowUnsafeUpdates = true;
docFolder.Files.Add(filename, Contents);
web.AllowUnsafeUpdates = false;
}
}

Add a Contenttype to a ListItem:
---------------------------------------------

using (SPSite sitecollection = new SPSite("http://..."))
{
using (SPWeb web = sitecollection.OpenWeb())
{
SPList list = web.Lists["document library"];

SPContentTypeCollection ctcollection = list.ContentTypes;
SPContentType contenttype = ctcollection[itemcontenttype];

SPListItem item = list.Items[0];
item["ContentTypeId"] = contenttype.Id;

web.AllowUnsafeUpdates = true;
item.Update();
web.AllowUnsafeUpdates = false;
}
}

Add an Eventhandler on a SharePoint list:
--------------------------------------------------
SPList list = web.Lists["List"];
SPEventReceiverType type = SPEventReceiverType.ItemAdded;
string assembly = "";
string className = "";
list.EventReceivers.Add(type, assembly, className);

list.Update();

Delete an EventHandler from a SharePoint list:
-------------------------------------------------
Guid guid = ...;

list.EventReceivers[guid].Delete();

Start a workflow on an item
-------------------------------------
Guid wfBaseId = new Guid("{6BE0ED92-BB12-4F8F-9687-E12DC927E4AD}");
SPSite site = ...;
SPWeb web = site.OpenWeb();
SPList list = web.Lists["listname"];
SPListItem item = list.Items[0];
SPWorkflowAssociation associationTemplate= list.WorkflowAssociations.GetAssociationByBaseID(wfBaseId);
site.WorkflowManager.StartWorkflow(item, associationTemplate, "");

Note: the workflow base ID can be found in the workflow.xml in the feature folder
Source: Steven Van de Craen

-------------------------------------------------

Other SharePoint Code Snippets...
Checking if a workflow is running on an item/SPWorkflowState
Detect an item published in ItemUpdating eventReceiver
How (not) to complete workflow tasks using code in WSS3
Modifying SharePoint Workflow Task Programmatically Problem
How to create MySite Programmatically
Add users of an AD Group to the SharePoint Site

3 comments:

Robin Meuré said...

Pretty handy mate! Thnx :)

Raghu said...

thanks for the code snippets.. i was looking just for it. I have some more here which am updating with all my work.
http://sharepointobjectmodel.blogspot.com/

vijai said...

Thanks for sharing :-)