1

I have created a custom content type called Certification and wish to mark my uploaded PDF as a certificate in certain circumstances.

The content type currently defaults to "Document".

This is my flow from start to finish.

First I load my content types and documents from my context.

ContentTypeCollection contentTypes = context.Web.ContentTypes;
context.Load(contentTypes);

List documents = context.Web.Lists.GetByTitle(DOCUMENTSLISTNAME);
context.Load(documents.RootFolder);

context.ExecuteQuery();

I then read in my PDF file and add it:

using (MemoryStream ms = new MemoryStream(System.IO.File.ReadAllBytes(@"blahblahpdf.pdf")))
{
       if (context.HasPendingRequest) { context.ExecuteQuery(); }

       var filename = "/blahblahpdf";

       string ServerURI = documents.RootFolder.ServerRelativeUrl;

       var filecreation = new FileCreationInformation();

       filecreation.ContentStream = ms;
       filecreation.Overwrite = true;
       filecreation.Url = ServerURI + filename;


       var file = files.Add(filecreation);

From here I set my ContentTypeId for the file:

var certType = contentTypes.FirstOrDefault(x => x.Name == "Certificate");

item["ContentTypeId"] = certType.Id.ToString();
item.Update();

context.ExecuteQuery();

My file is then uploaded, but has the Content Type of Document, not Certificate.

My certType does hold a correct result, and debugging after this point the items ContentTypeId field is correctly updated to my Certificate content types Id.

If I then obtain the new file using the context however the ContentTypeId has reverted back to that of Document.

I don't understand why my request is basically ignored. If I give an incorrect Id then no error is thrown, but I'm taking the Id directly from my ContentType collection.

Does anyone have any ideas what could be causing this issue?

  • 1
    shouldn't you be getting documents.ContentTypes as opposed to the Web.ContentTypes? – Tiago Duarte Nov 15 '16 at 12:49
0

try documents.ContentTypes as opposed to the Web.ContentTypes

See also

Site and List Content Types

When you add a site content type to a list or library, SharePoint Foundation makes local copies of any columns that are referenced by the content type and adds them to the list's fields. For more information, see Fields and Field References. In addition, SharePoint Foundation makes a local copy of the site content type and adds the copy to the content type collection on the list. The new list content type is a child of the site content type. The value of the Id property for the list content type is different from the value of the Id property for its parent site content type, but otherwise the two content types are initially the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.