0

I want to add some metadata to my modern Team Site.

I can achieve this by simply adding columns to the Site Pages library in the site contents.

Now I want to do the same thing programmatically using CSOM.

I use the following code for that purpose:

var pages = siteContext.Web.Lists.GetByTitle("Site Pages");
var fieldCreationInformation = new OfficeDevPnP.Core.Entities.FieldCreationInformation(fieldType)
{
    DisplayName = fieldName,
    InternalName = fieldName.Replace(" ", ""),
    AddToDefaultView = true,
    Required = false,
    Id = Guid.NewGuid()
};
pages.CreateField(fieldCreationInformation);

This works - the fields are getting added to the pages library, however they don't appear as the properties when I'm editing the page, unlike the columns added in the GUI.

Why? How can I add columns programmatically and see them as page properties?

3
  • Would you be open to using PowerShell PnP instead of CSOM? Aug 18, 2019 at 4:51
  • @DenisMolodtsov Well, yeah. Any help would be appreciated.
    – Szybki
    Aug 18, 2019 at 23:05
  • Added an answer with a PowerShell PnP used instead of CSOM. PowerShell PnP uses CSOM behind-the-scenes, though. Aug 18, 2019 at 23:35

2 Answers 2

1

For tasks like these, I highly recommend using PowerShell PnP. It's a fantastic, powerful and easy-to use PowerShell Module.

This is what you want to do in only 4 lines including Installation of the module:

Install-Module SharePointPnPPowerShellOnline
Connect-PnPOnline -Url https://tenant.sharepoint.com
$list = Get-PnPList -Identity "Site Pages"
Add-PnPField -List $list -Type Text -InternalName "Age" -DisplayName "Age" 

If you use PowerShell ISE or Visual Studio code - you get intellisense with autocomplete like so:

enter image description here

This is the result: enter image description here

Update: Adding field to the content type

You can also try adding existing field to the content type you need:

Add-PnPFieldToContentType -Field "Age" -ContentType " Wiki Page"
5
  • Unfortunately, this works in just about the same way as my CSOM code, which means that the field is added to the Site Pages library but it doesn't appear in page Properties in the Page details as you showed in the screenshot. I'm still getting information There are no custom properties available to add. Site owners can create properties by adding columns in the Pages library. in the Properties section. What could I possibly have done wrong?
    – Szybki
    Aug 20, 2019 at 22:12
  • I think I have a good idea what is wrong. Most likely you have several content types. Can you modify the View and display Content type so that you can see existing page's content types? Also, when you click on "new" - what are the options there? What are the available content types? Aug 20, 2019 at 23:26
  • 1
    I see. I have the following content types: Wiki Page, Web Part Page, Site Page, Repost Page and the Wiki Page is set as default. When I check Add to all content types option when creating a new column from gui I get the desired result, otherwise I won't see the column as a property.
    – Szybki
    Aug 21, 2019 at 15:01
  • So the solution here is to add the field not to the list, but to the desired content type directly. Aug 21, 2019 at 16:02
  • Updated the answer. Aug 21, 2019 at 16:11
0

We can use the CSOM C# code to achieve it.

string siteUrl="https://tenant.sharepoint.com/sites/lz";
string userName = "[email protected]";
string password = "xxx";

var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
var ctx = new ClientContext(siteUrl);
ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword);
List oList = ctx.Web.Lists.GetByTitle("Site Pages");

var schemaField = "<Field DisplayName='MyField' Type='Text' />";
Field oField = oList.Fields.AddFieldAsXml(schemaField, true, AddFieldOptions.DefaultValue | AddFieldOptions.AddToAllContentTypes);
ctx.ExecuteQuery();

More information:Creating fields using CSOM

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.