0

Below is my code

private void attachContentType(string siteURL, string path, string filename, ClientContext ctx, string folderName)
        {
            Web web = ctx.Web;
            Microsoft.SharePoint.Client.File home = ctx.Web.GetFileByServerRelativeUrl(web.ServerRelativeUrl + "/" + folderName + filename);
            ctx.Load(home);
            ctx.ExecuteQuery();
            home.ListItemAllFields["ContentTypeId"] = web.ContentTypes.GetByName("Page Layout").Id;
            home.ListItemAllFields["PublishingAssociatedContentType"] = web.ContentTypes.GetByName("Content Type Name").Name;
            home.ListItemAllFields.Update();
            ctx.ExecuteQuery();
        }

What I am trying to achieve is, set the type of the file to page layout, and set the associated content type to my custom content type.

This doesn't throw any exception, but when I edit the uploaded file it throws the below error

Failed to set value of the "Associated Content Type" column on the "Content Type ID" field type control

  • Please confirm you have entered your Content Type ID to the page layout in the format of 0x{CT_GUID}. – moe May 23 '17 at 6:24
  • Yeah @moe its in 0x format only – Vignesh Subramanian May 23 '17 at 6:29
  • 1
    try thins String.Format(";#{0};#{1};#", contentType.Name, contentType.StringId) for PublishingAssociatedContentType Field – TARUN May 23 '17 at 6:35
2

The format in PublishingAssociatedContentType should be ;#ct name;#ct id;#

Try below code:

var associatedContentType = web.ContentTypes.GetByName("Content Type Name");

String contentTypeAssociativeID = null;
if(associatedContentType != null)
{
    contentTypeAssociativeID = String.Format(";#{0};#{1};#", associatedCTName.Name, associatedCTName.StringId);
}

Web web = ctx.Web;
Microsoft.SharePoint.Client.File home = ctx.Web.GetFileByServerRelativeUrl(web.ServerRelativeUrl + "/" + folderName + filename);
ctx.Load(home);
ctx.ExecuteQuery();
home.ListItemAllFields["ContentTypeId"] = web.ContentTypes.GetByName("Page Layout").Id;
home.ListItemAllFields["PublishingAssociatedContentType"] = contentTypeAssociativeID;
home.ListItemAllFields.Update();
ctx.ExecuteQuery();

Set O365 PageLayout Associated Content type field

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.