user1872862
user1872862

Reputation: 79

SharePoint Online 365 and Web Site Title changing using CSOM

I have site created in SharePoint online. Changing the title using C# CSOM code updates the title of site and same gets retrieved properly.

But when I visit the site/refresh the site newly changed title is visible but again refreshing the page shows original title.

How this can be fixed.

I also tried changing title using "SetSiteProperties" function from - PnP-Sites-Core/Core/OfficeDevPnP.Core/Extensions/TenantExtensions.cs

The function SetSiteProperties throws exception. The site whose title needs to be changed is siteCollection of type TeamSite. Using CSOM or PnP-core, title change works only for Communication Site. For teamsite tile change is not working. Using CSOM it changes temporary only and with Pnp-core throws exception.

Upvotes: 0

Views: 1031

Answers (1)

Jerry
Jerry

Reputation: 3625

Update the Site Title Property using SharePoint Online CSOM like this:

static void Main(string[] args)
{
    string userName = "[email protected]";
    string password = "*************";
    var siteurl = "https://zheguo.sharepoint.com/sites/dev";
    var securePassword = new SecureString();
    foreach (char c in password)
    {
        securePassword.AppendChar(c);
    }
    using (var clientContext = new ClientContext(siteurl))
    {
        var onlineCredentials = new SharePointOnlineCredentials(userName, securePassword);
        clientContext.Credentials = onlineCredentials;
        Web web = clientContext.Web;
        clientContext.Load(web, a => a.ServerRelativeUrl);
        clientContext.ExecuteQuery();
        web.Title = "Website Properties Updated via CSOM";

        web.Update();
        clientContext.Load(web);
        clientContext.ExecuteQuery();


    }
}

Need to download install and reference SharePoint Online CSOM here:

SharePoint Online Client Components SDK

Result:

enter image description here

Upvotes: 0

Related Questions