3

Trying to use JSOM to update an existing ContentEditor on my SharePoint, I want to set a field value "WebUrl" to search another list item

   ExecuteOrDelayUntilScriptLoaded(
      function()
        {
var ctx = new SP.ClientContext();
var pageFile = ctx.get_web().getFileByServerRelativeUrl("/sites/Insight/fr-fr/ad/Pages/dm1A.aspx");
var webPartManager = pageFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var webPartDef = webPartManager.get_webParts().getById('996f8bd0-c26d-4774-9a39-e72c1cfa37d2');
var newSource = '~sitecollection/en-us/pms/';
var webPart = webPartDef.get_webPart();
ctx.load(webPart,'Properties');
ctx.executeQueryAsync(
  function () {
    var properties = webPart.get_properties();
    console.log(properties.get_fieldValues()['Title']);
    console.log(properties.get_fieldValues()['WebUrl']);
    properties.set_item("WebUrl", '~sitecollection/en-us/pms/');
    properties["WebUrl"] = "~sitecollection/en-us/pms/";
    webPartDef.saveWebPartChanges();
  },
  function(sender,args){
     console.log(args.get_message());
  });

        }, "sp.js");

Thank you

1 Answer 1

1

Here is the solution to fix issue : Add line : ctx.executeQueryAsync(function () {}, function () {});

Code:

ExecuteOrDelayUntilScriptLoaded(
      function()
        {
var ctx = new SP.ClientContext();
var pageFile = ctx.get_web().getFileByServerRelativeUrl("/sites/sight/fr-fr/ad/Pages/1A.aspx");
var webPartManager = pageFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var webPartDef = webPartManager.get_webParts().getById('996f8bd0-c26d-4774-9a39-e72c1cfa37d2');
var jsLinkUrl = '~sitecollection/en-us/ps/';
var webPart = webPartDef.get_webPart();
ctx.load(webPart,'Properties');
ctx.executeQueryAsync(
  function () {
    var properties = webPart.get_properties();
    console.log(properties.get_fieldValues()['Title']);
    console.log(properties.get_fieldValues()['WebUrl']);

      var urlS = properties.get_fieldValues()['WebUrl'];

      console.log(urlS);

    properties.set_item("WebUrl", '~sitecollection/ps/en-us/');
    //properties["WebUrl"] = "~sitecollection/en-us/ps/";
    webPartDef.saveWebPartChanges();
ctx.executeQueryAsync(function () {}, function () {}); // Add execute query

  },
  function(sender,args){
     console.log(args.get_message());
  });

        }, "sp.js");

Enjoy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.