0

Inside our sharepoint 2013 on-premises, i have the following code inside our server side item-added event receiver:-

public override void ItemAdded(SPItemEventProperties properties)
        {
base.ItemAdded(properties);
PublishingSite pSite = new PublishingSite(properties.Site);
SPContentType ctype = pSite.ContentTypes[contenttype];
PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true);
PageLayout pageLayout = pageLayouts[pagelayout];
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(properties.Web);
PublishingPageCollection pPages = pWeb.GetPublishingPages();
PublishingPage pPage = pPages.Add(newPageName + ".aspx", pageLayout);
//code goes here

the above code will create an enterprise wiki page which is linked to the related PageLayout/ContentType.

now i want to create an equivalent remote event receiver for the above code, so i did the following:-

private void HandleItemAdded(SPRemoteEventProperties properties)

        {

            using (ClientContext clientContext =

                TokenHelper.CreateRemoteEventReceiverClientContext(properties))

            {

                if (clientContext != null)

                {

                    try

                    {
                       PublishingSite pSite = new PublishingSite(clientContext);
                       SPContentType ctype = Site.ContentTypes[contenttype];

but the above code is raising errors on the 2 lines:-

  PublishingSite pSite = new PublishingSite(clientContext);
  SPContentType ctype = Site.ContentTypes[contenttype];

Errors:-

Error1  There is no argument given that corresponds to the required formal parameter 'objectPath' of 'PublishingSite.PublishingSite(ClientRuntimeContext, ObjectPath)'  

Error2 The type or namespace name 'SPContentType' could not be found (are you missing a using directive or an assembly reference?)  

so can anyone advice how i can covert the above server-side code to sharepoint client side object module?

1 Answer 1

0

You can create publishing page using CSOM C# code snippet below.

Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQueryRetry();

var layout = web.GetPageLayoutListItemByName("MyPageLayout");
ctx.Load(layout);
ctx.Load(layout, l => l.DisplayName);
ctx.ExecuteQueryRetry();

PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(ctx, web);
ctx.Load(publishingWeb);

PublishingPageInformation publishingPageInfo = new PublishingPageInformation();
publishingPageInfo.Name = "MyPage2.aspx";
publishingPageInfo.PageLayoutListItem = layout;

PublishingPage publishingPage = publishingWeb.AddPublishingPage(publishingPageInfo);

//publishingPage.ListItem.File.CheckOut();

ctx.Load(publishingPage);
ctx.Load(publishingPage.ListItem.File);
ctx.ExecuteQueryRetry();

ListItem listItem = publishingPage.ListItem;

listItem["MySiteColumn"] = "Test";


listItem.Update();

publishingPage.ListItem.File.CheckIn(string.Empty, CheckinType.MajorCheckIn);
publishingPage.ListItem.File.Publish(string.Empty);

ctx.Load(publishingPage);

ctx.ExecuteQueryRetry();

Reference: How To Create Publishing Pages Using Custom Page Layout Programmatically Using Client Side Object Model (CSOM) In SharePoint Office 365

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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