0

I am writing a CSOM to get the values of a managed metadata column (sourceWikiPage) and assign it to another page (newpage), here is my code:-

File sourceWikiPage = context.Site.RootWeb.GetFileByServerRelativeUrl("source.aspx");
context.Load(sourceWikiPage, items => items.ListItemAllFields);
context.ExecuteQuery();
PublishingPageInformation publishingPageInfo = new PublishingPageInformation();
publishingPageInfo.Name = newPageName+".aspx";
publishingPageInfo.PageLayoutListItem = layout;
PublishingPage pPage = pWeb.AddPublishingPage(publishingPageInfo);
ListItem newpage = pPage.ListItem;
newpage["Subjects"] = sourceWikiPage.ListItemAllFields["Subjects"] as TaxonomyFieldValue;//this does not have any effect!!
newpage.Update();
context.Load(newpage);
context.ExecuteQuery();

Now the page will be created, but the Subject managed metadata site column (which allow multiple values) will not have any values..

So can anyone advice?

1 Answer 1

1

Please refer the following code snippet to create publishing page and set multi-value managed metadata based on another item:

 using System;
 using System.Collections.Generic;
 using System.Linq;   
 using Microsoft.SharePoint.Client;
 using Microsoft.SharePoint.Client.Publishing;
 using Microsoft.SharePoint.Client.Taxonomy;

 public static string CreatePublishingPage(ClientContext context)
    {
        string pageLibrary = "Pages";
        string pageLayout = "EnterpriseWiki";
        string pageName = "MyNewPublishingPage.aspx";
        string pageDisplayName = pageName.Split('.')[0];
        string pageURL = string.Empty;

        try
        {

            Web webSite = context.Web;
            context.Load(webSite);
            PublishingWeb web = PublishingWeb.GetPublishingWeb(context, webSite);
            context.Load(web);

            if (web != null)
            {
                // Get Pages Library
                List pages = context.Site.RootWeb.Lists.GetByTitle(pageLibrary);
                ListItemCollection existingPages = pages.GetItems(CamlQuery.CreateAllItemsQuery());
                context.Load(existingPages, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == pageDisplayName));
                context.ExecuteQuery();

                // Check if page already exists
                if (existingPages != null && existingPages.Count > 0)
                {
                    // Page already exists
                    Console.WriteLine("Page already exists.\n");
                    pageURL = context.Web.ServerRelativeUrl + "/" + pageLibrary + "/" + pageName;
                }
                else
                {
                    // Get Publishing Page Layouts
                    Console.WriteLine("Creating Page...\n");
                    List publishingLayouts = context.Site.RootWeb.Lists.GetByTitle("Master Page Gallery");
                    ListItemCollection allItems = publishingLayouts.GetItems(CamlQuery.CreateAllItemsQuery());

                    context.Load(allItems, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == pageLayout));
                    context.ExecuteQuery();

                    ListItem layout = allItems.Where(x => x.DisplayName == pageLayout).FirstOrDefault();
                    context.Load(layout);
                    File tempWikiPage = context.Site.RootWeb.GetFileByServerRelativeUrl("/sites/dev/Pages/Temp.aspx");
                    context.Load(tempWikiPage,items => items.ListItemAllFields);
                    context.ExecuteQuery();
                    // Create a publishing page
                    PublishingPageInformation publishingPageInfo = new PublishingPageInformation();
                    publishingPageInfo.Name = pageName;
                    publishingPageInfo.PageLayoutListItem = layout;

                    PublishingPage publishingPage = web.AddPublishingPage(publishingPageInfo);

                    TaxonomyFieldValueCollection sourceValue = tempWikiPage.ListItemAllFields["Subjects"] as TaxonomyFieldValueCollection;
                    string[] termValuesarrary;
                    List<string> termValues = new List<string>();
                    foreach (TaxonomyFieldValue taxProductFieldValue in sourceValue)
                    {
                        termValues.Add("-1;#" + taxProductFieldValue.Label + "|" + taxProductFieldValue.TermGuid);
                    }
                    termValuesarrary = termValues.ToArray();
                    string termValuesstring = string.Join(";#", termValuesarrary);
                    FieldCollection fields = context.Site.RootWeb.Lists.GetByTitle("Pages").Fields;
                    context.Load(fields);
                    context.ExecuteQuery();
                    Field SubjectField = fields.GetByTitle("Subjects");
                    context.Load(SubjectField);
                    context.ExecuteQuery();
                    TaxonomyField txField = context.CastTo<TaxonomyField>(SubjectField);
                    var newtermValues = new TaxonomyFieldValueCollection(context, termValuesstring, txField);
                    txField.SetFieldValueByValueCollection(publishingPage.ListItem, newtermValues);
                    publishingPage.ListItem["Title"] = pageDisplayName;
                    publishingPage.ListItem["Standard"] = tempWikiPage.ListItemAllFields["Standard"];
                    publishingPage.ListItem.Update();
                    publishingPage.ListItem.File.CheckIn(string.Empty, CheckinType.MajorCheckIn);
                    publishingPage.ListItem.File.Publish(string.Empty);
                    context.Load(publishingPage);
                    context.Load(publishingPage.ListItem.File, obj => obj.ServerRelativeUrl);
                    context.ExecuteQuery();

                    pageURL = context.Web.ServerRelativeUrl + "/" + pageLibrary + "/" + pageName;
                    Console.WriteLine("Page Created.\n");
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Failed. Error: " + e);
        }
        return pageURL;
    }

enter image description here enter image description here

1
  • worked well....
    – John John
    Commented Dec 27, 2019 at 10:20

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.