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?