I have an issue regarding Content Types,I want to set a particular Content Type for each document set within a library, however SharePoint 2013 defaults to the library's default CT. The issue is described here: Document Sets don't enforce allowed Content Types when multiple Doc Set CT are available

From what I can see I need to add code to an event receiver, however I am having problems getting this to work properly. The closest result I got to was:

public override void ItemAdded(SPItemEventProperties properties)
        properties.ListItem["ContentType"] = "Strategy";

However I want the form properties for the correct Content Type to display when the document is uploaded, this code updates after the document is saved. It also throws an error "the file .... has been modified by ....". But it does make the change.

I've also tried several variations of the code below and continuously received "Object reference not set to an instance of an Object.

public override void ItemAdding(SPItemEventProperties properties)

        SPList list = properties.List;

        SPFile file = properties.ListItem.File;
        SPListItem item = file.Item;
        item["ContentType"] = "Strategy";


In addition to this I also had some code that received an error along the lines of "To add a document to a library you must use SPFileCollection.Add()"

I took some inspiration from this method as well, and still couldn't quite get my code to set the Content Type before the Edit form is displayed.

 protected bool CreateDocument( string sFilename, string sContentType, string sList)

{ using (SPSite site = new SPSite("http://intranet.mysite.com")) {

         using (SPWeb web = site.OpenWeb())

      SPList list = web.Lists[sList];
      // this always uses root folder
      SPFolder folder = web.Folders[sList];
      SPFileCollection fcol = folder.Files;

      // find the template url and open
      string sTemplate =list.ContentTypes[sContentType].DocumentTemplateUrl;
      SPFile spf = web.GetFile(sTemplate);
      byte[] binFile = spf.OpenBinary();
      // Url for file to be created
      string destFile = fcol.Folder.Url + "/" + sFilename;

      // create the document and get SPFile/SPItem for 
      // new document
      SPFile addedFile = fcol.Add(destFile, binFile, false);
      SPItem newItem = addedFile.Item;
      newItem["ContentType"] = sContentType;
        return true;
 catch (SPException spEx)
    // file already exists?
    if (spEx.ErrorCode == -2130575257) 
        return false;
        throw spEx;

(I know this isn't a solution for my problem, but I tried to edit the code and didn't get very far)

This is my first attempt at event receivers so I'm struggling a bit to get my head around it. I also read that BeforeProperties and AfterProperties aren't relevant to Document Libraries. Any advice would be much appreciated, I've spent a day trying to work through what I assume is probably extremely simple!

1 Answer 1


Frustratingly simple... it took me a while but I got there thanks to this post: https://christopherclementen.wordpress.com/2012/04/30/get-field-value-during-an-event-handler/

I ended up using ItemAdded, but in order for it to add before the form loads I had to add: <Synchronization>Synchronous</Synchronization>

<Synchronization>Synchronous</Synchronization>   <------

Then the code was easy and already outlined above...

public override void ItemAdded(SPItemEventProperties properties)
    properties.ListItem["ContentType"] = "Strategy";

It's only easy when you know the answer...

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.