0

I'm trying to set a SharePoint site's Title through the SharePoint REST API using C# code.

When I get the site's web data using:

GET http://mycompany.sharepoint.com/sites/site-url/_api/web/

I get a response something like this:

{
    "d": {
        "__metadata": {
            "id": "https://mycompany.sharepoint.com/sites/site-url/_api/Web",
            "uri": "https://mycompany.sharepoint.com/sites/site-url/_api/Web",
            "type": "SP.Web"
        },
        // .... many more nodes here .....
        "Title": "my old title to be changed",
        // ... more data here
    }
}

Now I was hoping to change that SharePoint site's title by using this REST command:

POST http://mycompany.sharepoint.com/sites/site-url/_api/web

with a body of

{
    "__metadata": {
        "type": "SP.Web"
    },
    "Title": "My new SharePoint site title here"
}

and headers:

Accept              application/json;odata=verbose
Content-Type        application/json;odata=verbose
X-HTTP-Method       MERGE
If-Match            *

The command executed from Postman completes with an HTTP 200 - OK status - yet, when I go retrieve the title again:

GET http://mycompany.sharepoint.com/sites/site-url/_api/web/title

I still get back:

{
    "d": {
        "Title": "my old title to be changed",
    }
}

So it seems nothing really was changed at all ...

How can I change a SharePoint Online site's Title property through a REST API call?

7
  • What permissions do you have on the site? Commented Oct 4, 2023 at 8:52
  • The user is the owner of the site collection, and is also member of the site collection adminstrators group globally on our tenant. Therefore I really don't think it's a permission issue ...
    – marc_s
    Commented Oct 4, 2023 at 9:07
  • Owner should be enough or site admin. SharePoint administrator role (tenant) doesn't automatically give you access to all sites by the way, you have to give yourself access to each site. Commented Oct 4, 2023 at 9:22
  • Personally, if I was building an app in C#, I would just use the PnP Core SDK pnp.github.io/pnpcore/index.html or the newer Graph API wherever possible. Commented Oct 4, 2023 at 9:32
  • @CallumCrowley: can you show me how to do this with the MS Graph API? I've been able to retrieve some information - but on the root of my site, the only thing I see is the displayName - but that property is read-only - so what do I need to do to set the title to a new value?? Can you point to a blog post or post an answer here? I have a really hard time finding any meaningful samples of how to use the MS Graph with Sharepoint....
    – marc_s
    Commented Oct 4, 2023 at 12:54

2 Answers 2

0

I haven't tried the techniques described in this blog post, but it's possible it may help you.

How To Change The Site Title Of Modern SharePoint Team Site

Blog post

0

You can use SharePoint REST API call like below to update the SharePoint online site Title:

<script type="text/javascript" src="/sites/SPConnect/SiteAssets/jquery.min.js"></script>

<button type="button" onclick="updateSiteTitle()"> Update Site Title </button>

<script type="text/javascript">
    function updateSiteTitle() {
        var updateSiteTitleEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web";

        var payload = JSON.stringify({
            '__metadata': { 'type': 'SP.Web' },
            'Title': 'SPConnect - REST'
        });

        $.ajax({
            url: updateSiteTitleEndpoint,
            type: "POST",
            data: payload,
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-HTTP-Method": "MERGE",
                "If-Match": "*"
            },
            success: function (data) {
                console.log("Site Title updated successfully!");
            },
            error: function (error) {
                console.log(error);
            }
        });
    }
</script>

I have just tried using it and it worked for me. You can adjust and use above code as per your requirements.

Output:

enter image description here

Reference: Update Propertybag values in sharepoint using REST API

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.