Utwórz stronę witryny za pomocą REST API

Aug 22 2020

Chciałbym utworzyć stronę witryny za pośrednictwem REST API z własnym tytułem i zawartością. Do tej pory próbowałem wysłać żądanie POST, ale pojawił się błąd:

Aby dodać element do biblioteki dokumentów, użyj SPFileCollection.Add ()

POCZTA: <url>/_api/web/lists/GetByTitle('Site Pages')/Items

Ciało:

{
  "__metadata": {
    "type": "SP.Data.SitePagesItem"
  },
  "Title": "It's working?"
}

Z tego co rozumiem - najpierw muszę stworzyć Stronę Serwisu (plik) w SharePoint, a następnie zmodyfikować ją przez REST. Jednak moim wymaganiem jest utworzenie całego wpisu tylko przez REST.

Jak można to zrobić?

Odpowiedzi

1 spDevAdmin Aug 22 2020 at 18:26

To świetny artykuł wyjaśniający, jak to zrobić. Zasadniczo tworzy nowy .aspxplik w bibliotece stron witryny. Oto kod, którego używają (wywoływany ze strony w docelowej sieci):

function CreateWikiPage() {

// Get Server relative url of Web(site)
var WebServerRelativeUrl = _spPageContextInfo.webServerRelativeUrl;

// Provide Internal name of the library here
var DocuentLibraryInternalName = "SitePages";

// Provide name of the wiki page to be created
var NewPageName = "NewRESTWikipage.aspx";

// Form relative url of the new page. This will be used in function below
var NewFileUrl = WebServerRelativeUrl + "/" + DocuentLibraryInternalName + "/" + NewPageName;

$.ajax({ // "templateFileType" values in below method // StandardPage. The value = 0. // WikiPage. The value = 1. // FormPage. The value = 2. url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('" + WebServerRelativeUrl + "/" + DocuentLibraryInternalName + "')/Files/AddTemplateFile(urlOfFile='" + NewFileUrl + "',templateFileType=1)", method: "POST", headers: { "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data, status, xhr) {
        console.log("Success");
    },
    error: function (xhr, status, error) {
        console.log("Failed");
    }
});
}
Sokołow Aug 25 2020 at 15:05
  1. Utwórz szablon „nowoczesnych wpisów” w programie SharePoint. Postępuj zgodnie z tym przewodnikiem, aby to zrobić.

  2. Utwórz nową stronę witryny za pośrednictwem REST API, korzystając z tego zapisanego szablonu. Po prostu skopiuj go z folderu szablonów do folderu głównego tej podstrony:/_api/Web/GetFileByServerRelativeUrl('<source_url>')/CopyTo(strnewurl='<target_url>',bOverwrite=false)

  3. Edytuj nowo utworzoną Stronę Serwisu, jak chcesz (np. Przez REST API).

Źródłowe i docelowe adresy URL:

source_url - /sites/<your_site>/SitePages/Templates/Your-Template.aspx

target_url - /sites/<your_site>/SitePages/New-Modern-Page.aspx