I want to update some custom WebPart properties of a visual WebPart. Right now, I'm able to update the correct WebPart by checking it's title. Here's what I mean:

function GoToGalleryWebPartPage(pageNo) {
    var currentCtx = SP.ClientContext.get_current();
    var pageFile = currentCtx.get_web().getFileByServerRelativeUrl(_spPageContextInfo.serverRequestPath);
    var webPartManager = pageFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
    var webPartDefs = webPartManager.get_webParts();
    currentCtx.load(webPartDefs, 'Include(WebPart.Properties)');
    currentCtx.executeQueryAsync(function () {          
        if (webPartDefs.get_count()) {
            for (var i = 0; i < webPartDefs.get_count() ; i++) {
                var webPartDef = webPartDefs.getItemAtIndex(i);
                var webPartSingle = webPartDef.get_webPart();       
                var properties = webPartSingle.get_properties();

                // Check the title of the webpart to see if it's the webpart we want.
                if (properties.get_fieldValues()['Title'] == "My Web Part") {
                    properties.set_item("PageNo", pageNo);

                    currentCtx.executeQueryAsync(success, failure);

However, this becomes a problem when there are two instances of the same WebPart on the same page as they will have the same title.

I'm thinking of using ClientID instead as it is unique. However, I can't seem to find a way no matter how hard I try. Is there even a way to get the clientID of webparts?

If you can suggest better/alternative ways of solving the problem, please let me know as well.

Your Answer

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

Browse other questions tagged or ask your own question.