Quick dhe Easy: Krijo një pushim SharePoint Site Using

There are a lot of resources around that show how to do this, but I couldn’t find a comprehensive go-to link, so here we are.

You can create a SharePoint site using the REST API.  Here’s a fully baked example:

<!--
    SiteRequestForm.html: Collect information and create a site for the user.
-->

<qendër>
<tryezë>
    <tr>
        <td>Site Name:</td>
        <td><të dhëna lloj="text" emër="SiteName" id="SiteName" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <të dhëna lloj="submit" id="CreateSiteButton" vlerë="Create the Site" />
        </td>
    </tr>
</tryezë>
</qendër>

<dorëshkrim src="../Plugins/jquery-1.11.0.min.js"></dorëshkrim>

<dorëshkrim>
var CreateSiteLogicContainer = {

    createSiteData: {
            "parameters": {
                __metadata: { "type": "SP.WebInfoCreationInformation" },
                Url: "Paultest1",
                Titull: "Paultest1",
                Përshkrim: "rest-created web by Paul!",
                Language: 1033,
                WebTemplate: "sts",
                UseUniquePermissions: i rremë
            }
    },

    createSite: funksion () {

        jQuery.support.cors = i vërtetë;

        CreateSiteLogicContainer.createSiteData.parameters.Url = $("#SiteName").val();
        
        $.ajax({
            url: "https://bigapplesharepoint.sharepoint.com/NBAIADev/_api/web/webinfos/add",
            metodë: "POST",

            headers: {
                "Accept": "application/json; odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },

            të dhëna: JSON.stringify(CreateSiteLogicContainer.createSiteData),

            success: funksion () { vigjilent("success"); },
            gabim: funksion () { vigjilent("error"); }

        });
    },

    wireUpForm: funksion () {
        $("#CreateSiteButton").klikoni(funksion () {
            vigjilent("About to try and create the site.");
            CreateSiteLogicContainer.createSite();
        });
    }


}

CreateSiteLogicContainer.wireUpForm();

</dorëshkrim>

When successful, you get a JSON packet in response like this:

image

My key thoughts and learnings from this include:

  • This approach uses jQuery.  Në rastin tim, my jQuery library is located in “../plugins.”  You’ll want to change that to point to your favorite JQ location.
  • You can copy and paste that whole snippet into a Content Editor Web Part on a page and it should work just fine.  You’ll want to change the end point of the API call and make sure you reference JQ correctly.
  • The URL is relative to your API’s endpoint.  Në rastin tim, it’s creating sub-sites underneath https://bigapplesharepoint.com
  • You don’t need to provide a content-length. Some blog posts and MSDN document implies that you do, but happened for me automatically, which I assume is being handled by the $.ajax call itself.
  • This line is required in order to avoid a “forbidden” response: "X-RequestDigest": $("#__REQUESTDIGEST").val().  There are other ways to do it, but this is pretty nice.  I have lost the link to blog that provided this shortcut.  H/T to you, mysterious blogger!

Good luck and hope this helps someone out.

</fund>

undefinedAbonohen në blogun tim.

Atëherë ejani pas meje në Twitter në http://www.twitter.com/pagalvin

Lini një Përgjigju

Adresa juaj e emailit nuk do të publikohet. Fusha e kërkuar janë shënuar *