3
votes

I am creating a web part that uses jQuery to do a REST API call to rename the SharePoint subsite. This works when using a custom button on EditForm.aspx on a list for example. It also works using Fiddler. However, I am getting an HTTP 400 Bad Request when trying to do this with a custom button in a web part.

Here is my code:

<script type="text/javascript" src="https://mysite.mysharepointsite.com/sites/vsdev/SiteAssets/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="https://mysite.mysharepointsite.com/sites/vsdev/SiteAssets/jquery.SPServices-2014.01.min.js"></script>

<script type="text/javascript">

function RenameSubsite()
{

$.ajax({
    url: "https://mysite.mysharepointsite.com/sites/vsdev/_api/web/",
    type: "POST",
    data: JSON.stringify({ '__metadata': { 'type': 'SP.Web' }, 'ServerRelativeUrl': "/vsdev2" }),
    headers: {
        "accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",
        "content-length": "225",
        "X-HTTP-Method": "MERGE",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "IF-MATCH": "*"
    },
    success: function (return_data, textStatus) {
        alert('Successfully Renamed the Site!');
        return;

    },

    error: function (xhr, textStatus, errorThrown) {
        alert('Error! - Status: ' + textStatus + ' Desc: ' + errorThrown);
        return;
    }
});

}
</script>

<p>
<input type="button" value="Rename Subsite to vsdev2" onclick="RenameSubsite()" />
</p>

Pretty straight forward REST API call...but it is not working from the web part..

UPDATE - Updating the 'Title' instead of the 'ServerRelativeUrl' works fine. I have tried all types of variations of the value to update the ServerRelativeUrl to as well.

1

1 Answers

0
votes

It's impossible to update ServerRelativeUrl property. It's fixed once the site collection is created. You cannot manipulate this value via JSOM/CSOM.

The only way to change the URL of a site collection is to execute a server-side code: PowerShell (or other cmdlets like Copy-SPSite, Export-SPSite, Move-SPSite, etc.) or SSOM.