Azkarra eta erraza: Sortu SharePoint Web bat REST erabiliz

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.
-->

<zentro>
<taula>
    <tr>
        <td>Site Name:</td>
        <td><sarrera mota="text" izena="SiteName" id="SiteName" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <sarrera mota="submit" id="CreateSiteButton" balioa="Create the Site" />
        </td>
    </tr>
</taula>
</zentro>

<script src="../Plugins/jquery-1.11.0.min.js"></script>

<script>
izan zen CreateSiteLogicContainer = {

    createSiteData: {
            "parameters": {
                __metadata: { "type": "SP.WebInfoCreationInformation" },
                Url: "Paultest1",
                Izenburua: "Paultest1",
                Deskribapena: "rest-created web by Paul!",
                Hizkuntza: 1033,
                WebTemplate: "sts",
                UseUniquePermissions: faltsuak
            }
    },

    createSite: funtzioa () {

        jQuery.support.cors = Egia;

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

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

            data: JSON.stringify(CreateSiteLogicContainer.createSiteData),

            success: funtzioa () { ohartaraztea("success"); },
            error: funtzioa () { ohartaraztea("error"); }

        });
    },

    wireUpForm: funtzioa () {
        $("#CreateSiteButton").sakatu(funtzioa () {
            ohartaraztea("About to try and create the site.");
            CreateSiteLogicContainer.createSite();
        });
    }


}

CreateSiteLogicContainer.wireUpForm();

</script>

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

image

My key thoughts and learnings from this include:

  • This approach uses jQuery.  Nire kasuan, 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.  Nire kasuan, 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.

</amaiera>

undefinedNire blog Harpidetu.

Follow me on Twitter http://www.twitter.com/pagalvin

Utzi iruzkin bat

Zure e-posta helbidea ez da argitaratuko. Beharrezko eremuak markatu dira *