3

This is a follow up question to the one i asked earlier : Reading web part properties using javascript object model

In my earlier question I had been able to access the properties using jsom but i am unable to target a particular web part without knowing its ID before hand. Inspecting the DOM i have found the below html. Here, the attribute webpartid is supposed to be used while performing a getById(GUID) call.

<div id="WebPartctl00_ctl35_g_a70dc106_4ee6_4a5d_9d85_95a2aabcffb1" webpartid2="a70dc106-4ee6-4a5d-9d85-95a2aabcffb1" webpartid="5f8931ff-a930-4942-be06-0fe948ad8c4c">

It would be great if anyone could help me out. Thanks in advance.

2 Answers 2

4

You can use jQuery (or even plain JavaScript) to get the attribute from the generated for the wev part.

The only thing you'd need is somethint that allows you to identify this Web Part even from within the main container.

If you own the code for the WP, you can add a custom div with and ID or a class (or data-something), get that using jQuery and then navigate to thr parent container and get the webpartid property.

If it's an OOB web part you need to identify something unique about it, such as a special ID, class, etc. And follow the same logic.

Additionally, please review the following post as it explains how to get ALL the WPs in the page, so you could easily find it on the collection using JavaScript.

https://msdn.microsoft.com/en-us/library/office/hh185010%28v=office.14%29.aspx

2
  • So basically the data-something or unique element is sort of a hack. Not a clean way to implement. Was hoping there would be some proper method to accomplish this.
    – Tanmay
    Feb 13, 2015 at 12:09
  • 1
    The only way would be if you programmatically control the addition of the Web Part. In that case you can set the webpartid to whatever you want and then use that identifier in your query. Feb 13, 2015 at 12:19
1

Hi Tanmay Your Web Part will have an element attribute called webpartid so you can use this to select all Web Parts, like this (jQuery):

$('[webpartid]');
1
  • My question was more towards finding the webpartid. I might have multiple webpartid attributes on a page. How do i know which ID belongs to the web part i want
    – Tanmay
    Feb 13, 2015 at 12:11

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.