0

I am trying to attach a content type to a page layout

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;
            ctx.ExecuteQuery();
        }

This does not throw any error, but when I edit properties of the pagelayout, its still showing Design File only

  • 1
    I think home.ListItemAllFields.Update(); was missed. – TARUN May 23 '17 at 5:56
  • Added my solution as answer – TARUN May 23 '17 at 6:19
1

I think you missed home.ListItemAllFields.Update();

home.ListItemAllFields["ContentTypeId"] = web.ContentTypes.GetByName("Page Layout").Id;
home.ListItemAllFields.Update();
ctx.ExecuteQuery();
| improve this answer | |
1

Try the below code. Note, it will only work on the RootWeb, so you might need to the change the web object accordingly, somewhat like Web web = ctx.Site.RootWeb:

Web web = ctx.Web;
List list = web.Lists.GetByTitle("Master Page Gallery");
ContentTypeCollection contentTypeColl = list.ContentTypes;
ctx.Load(contentTypeColl);
ctx.ExecuteQuery();

ContentType contentType=null;
foreach (ContentType ct in contentTypeColl)
{
    if (ct.Name == "Page Layout")
    {
        contentType = ct;
        break;
    }   
}

Microsoft.SharePoint.Client.File home = ctx.Web.GetFileByServerRelativeUrl(web.ServerRelativeUrl + "/" + folderName + filename);
ctx.Load(home);
ctx.ExecuteQuery();
home.ListItemAllFields["ContentTypeId"] = contentType.Id.ToString();
home.ListItemAllFields["Content Type"] = contentType.Name;
home.ListItemAllFields.Update();
ctx.ExecuteQuery();
| improve this answer | |

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.