0

I am working on a script, it's in AngularJS, but it could be jQuery or just JavaScript for the sake of this question. The script is inside a Content Editor Web Part. I want to provide configuration data to the script so it can be reused easily. In this particular case, I want to search and display the contents of a SharePoint List, but I don't want to hard-code the name of the List in the script.

When I was working with C#, I could easily modify the Properties pane. But for this assignment, I am working with script. I know the script is aware of of the URL, and I could pass the List name with a query string. But I am looking for a way to read a property from the Content Editor Web Part. Is that possible?

UPDATE: To make myself more clear, I want a reusable script I can put in ten different Web Parts. In each one of those Web Parts, I want to set a property of the Web Part that the script reads. So, WITHOUT rewriting the script, I configure it to read a different list in each Web Part.

  • 1
    Which property are you trying to read? Since CEWP is out of the box it is not possible to create a new property as such. – Amal Hashim Oct 26 '17 at 11:44
  • I would be happy with using existing properties, the ones you can set in the Properties panel. Even the Title field would be sufficient. If you can provide an example of how to read any properties, it would be enough to get me going. – CigarDoug Oct 26 '17 at 11:54
1

You can use below code to get the webpart titles from the current page

var ctx = new SP.ClientContext();
var page = ctx.get_web().getFileByServerRelativeUrl('/sites/dev/SitePages/Home.aspx');
var wm = page.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var webparts = wm.get_webParts();
ctx.load(webparts,'Include(WebPart)');
ctx.executeQueryAsync(
    function () {
        for(var i = 0;i < webparts.get_count();i++) {
           var webpart = webparts.getItemAtIndex(i);
           var webPart = webparts.get_webPart();
           alert(webpart.get_title());
        }
    },
    function(sender,args){
        console.log(args.get_message());
    }
);
  • OK, but which one is my parent Web Part? I can walk my way up through the parent of each element to get to the Web Part, but I can't figure out how to get any of it's Properties. – CigarDoug Oct 26 '17 at 12:36
  • Find the webpart id using browser developer tool? Then you can query for only that webpart using var webpart = wm.get_webParts().getById('f0cd71f8-6fac-4a9c-ad45-4b239563631c'); – Amal Hashim Oct 26 '17 at 12:38
  • I don't think I am getting across what I want. On the fly, within the script, I want the script to determine the parent Web Part it is in, and read the properties of THAT Web Part. So I can drop the same script in ten different web parts, set the property in each, and be done. – CigarDoug Oct 26 '17 at 12:56
  • 1
    The code I gave you will get all webparts in the page you have configured. You might need to use another property to figure out the parent webpart and then read the title of that webpart. – Amal Hashim Oct 26 '17 at 13:09
  • That's the part I am missing, getting the properties of the PARENT web part. – CigarDoug Oct 26 '17 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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