2

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)
    {
        base.ItemAdded(properties);
        properties.ListItem["ContentType"] = "Strategy";
        properties.ListItem.Update();
    }

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)
    {
        base.ItemAdding(properties);

        SPList list = properties.List;

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




    }

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")) {

     try{
         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;
      newItem.Update();
      addedFile.Update();
        return true;
            }
     }
 catch (SPException spEx)
{
    // file already exists?
    if (spEx.ErrorCode == -2130575257) 
        return false;
    else
        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

3

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>

<Receiver>
<Name>EventReceiver1ItemAdded</Name>
<Type>ItemAdded</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>TutoTaxonomy.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
<Synchronization>Synchronous</Synchronization>   <------
</Receiver>

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

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

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.