Retrieve Publishing image field with Sharepoint 2013 REST Api / CSOM

后端 未结 4 822
忘掉有多难
忘掉有多难 2021-02-06 01:25

We\'re using the Sharepoint 2013 REST API to get all news items from the Sharepoint. We made a custom ContentType \'Newsitem\' with several properties including a Publishing Ima

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 02:19

    I found a way to get all data in a single call: use the RenderListDataAsStream POST REST call:

    this._webAbsoluteUrl +
    "/_api/web/GetList(@listUrl)/RenderListDataAsStream?
    @listUrl=%27%2Fsites%2F%2Flists%2F%27";
    

    It gives all simple text field data, can be used for more complex fields such as managed metadata & lookup fields, but more importantly in this case, it gives you the url of the image that was used for the Publishing Image column.

    You do need to parse it from this structure:

    "
    "

    Code sample (SPFx with TypeScript):

    const requestHeaders: Headers = new Headers();
    requestHeaders.append("Content-type", "application/json");
    requestHeaders.append("Accept", "application/json");
    
    var body =
    { parameters:
      {
        DatesInUtc: "true"
      }
    };
    
    const httpClientOptions: IHttpClientOptions = {
      body: JSON.stringify(body),
      headers: requestHeaders
    };
    const queryUrlGetAllItems: string =
      this._webAbsoluteUrl +
      `/_api/web/GetList(@listUrl)/RenderListDataAsStream?@listUrl=%27%2Fsites%2F%2Flists%2F%27`;
    
    return this._webPartContext.spHttpClient
      .post(queryUrlGetAllItems, SPHttpClient.configurations.v1, httpClientOptions)
      .then((response: any) => {
        if (response.status >= 200 && response.status < 300) {
          return response.json();
        } else {
          return Promise.reject(new Error(JSON.stringify(response)));
        }
      })
      .then((data: any) => {
        if (data) {
          for (let i = 0; i < data.Row.length; i++) {
            let item = data.Row[i];
            // ... process data
          }
        }
        // ... return value
      });
    

提交回复
热议问题