Pregunta

I'm writing an SPFx web part using React. I can get the description of the current web like this:

let desc = this.props.context.pageContext.web.description;

But how can I change the web description? The context object above is read-only, but I should be able to use a REST call using PnP or some JSOM-stuff. I've tried both with no luck, but I'm no expert and could use an example showing how to do it.

I'm on SharePoint 2019 (on premise).

¿Fue útil?

Solución

You can use PnP JS to update the Web Properties. You need to set the Description property of web.

Sample Example:

import { Web } from "@pnp/sp";

let web = new Web("https://my-tenant.sharepoint.com/sites/mysite");

web.update({
    Title: "New Title",
    Description: "My new description",
}).then(w => {
    console.log(w);
});

Source: PnP JS - Update Web Properties


Using REST API:

Example:

$.ajax({
  url: "http://<site url>/_api/web",
  type: "POST",
  data: "{ '__metadata': { 'type': 'SP.Web' }, 'Description': 'My new description'}",
  headers: { 
    "X-RequestDigest": <form digest value>,
    "accept": "application/json;odata=verbose",
    "content-type": "application/json;odata=verbose",
    "content-length": <length of body data>,
    "X-HTTP-Method": "MERGE"
  },
  success: successHandler,
  error: errorHandler
});

Microsoft documentation: Webs REST API reference

Licenciado bajo: CC-BY-SA con atribución
scroll top