I am working on a console application which uses CSOM to integrate with SharePoint online. and i want to create a publishing page, so i wrote this code, to get the PageLayout and create a new Publishing page, based on the values of other page:-
File tempWikiPage = context.Site.RootWeb.GetFileByServerRelativeUrl(scurl4 + tempItemTitle + ".aspx");
context.Load(tempWikiPage,items => items.ListItemAllFields);
context.ExecuteQuery();
List publishingLayouts = context.Site.RootWeb.Lists.GetByTitle("Master Page Gallery");
CamlQuery camlQuery4 = new CamlQuery();
camlQuery4.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/>" +
"<Value Type='String'>Basic Page</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>";
ListItemCollection allItems = publishingLayouts.GetItems(camlQuery4);
context.Load(allItems,
items => items.Include(
item => item["Title"]
));
context.ExecuteQuery();
ListItem layout = allItems.FirstOrDefault();
PublishingPageInformation publishingPageInfo = new PublishingPageInformation();
publishingPageInfo.PageLayoutListItem = layout;
PublishingPage pPage = pWeb.AddPublishingPage(publishingPageInfo);
ListItem newpage = pPage.ListItem;
newpage["Title"] = newPageName;
newpage["PublishingPageContent"] = r;
newpage["Standard"] = tempWikiPage.ListItemAllFields["Standard"];
now i am facing these 2 issues:-
the
layout
will be null.once the code reaches
newpage["Standard"] = tempWikiPage.ListItemAllFields["Standard"];
i got this exception:-
The object is not associated with an object identity or the object identity is invalid.
Microsoft.SharePoint.Client.ClientRequestException was caught
HResult=-2146233088 Message=The object is not associated with an object identity or the object identity is invalid.
Source=Microsoft.SharePoint.Client.Runtime StackTrace: at Microsoft.SharePoint.Client.DataConvert.WriteValueToXmlElement(XmlWriter writer, Object objValue, SerializationContext serializationContext) at Microsoft.SharePoint.Client.ClientActionInvokeMethod.WriteToXmlPrivate(XmlWriter writer, SerializationContext serializationContext) at Microsoft.SharePoint.Client.ClientActionInvokeMethod..ctor(ClientObject obj, String methodName, Object[] parameters) at Microsoft.SharePoint.Client.ListItem.SetFieldValue(String fieldName, Object value) at Microsoft.SharePoint.Client.ListItem.set_Item(String fieldName, Object value)