4

How to add a web part on page in such a way that we later on can find it (using CSOM) - Can we somehow store identifier information to the web part? Say user could change the title and description (so they cannot be used as identifiers).

Or do we have to use (asmx) web services..

Edit

Appears asmx is needed. Vadim proposes JavaScript solution below. Here also simple server-side code (client uses _vti_bin/WebPartPages.asmx)

XElement resultElement = client.GetWebPartProperties2("/SitePages/DevHome.aspx", Storage.Shared, SPWebServiceBehavior.Version3);

var webParts = new List<Dictionary<string,string>>();
foreach (XNode webPartNode in resultElement.Nodes())
{
  var properties = new Dictionary<string,string>();
  XElement element = webPartNode as XElement;
  foreach (XNode propertyNode in element.Nodes())
  {
    XElement propertyElement = propertyNode as XElement;
    if (propertyElement != null)
    {
      properties.Add(propertyElement.Name.LocalName, propertyElement.Value);
    }
  }
  webParts.Add(properties);
}

Edit2 As Ali points below we could use web part guid as the identifier: we would get it while creating the web part, store it to an external location, and use it later on. However, in this case we cannot use/store this identifier but need to provide our own. Thus the only remaining possibility seems to be asmx.

2

WebPartPagesWebService exposes GetWebPartProperties2 Method that could be used to retrieve information about web parts (eg, WebPartID) on page

For example, the code below demonstrates how to get Web Parts Ids:

function getWebPartProperties(pageUrl,completeFn,errorFn)
{
    var soapEnv =
        '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  \
            <soap:Body>  \
                <GetWebPartProperties2 xmlns="http://microsoft.com/sharepoint/webpartpages">  \
                    <pageUrl>' + pageUrl + '</pageUrl>   \
                    <storage>Shared</storage>   \
                    <behavior>Version3</behavior>   \
                </GetWebPartProperties2>  \
            </soap:Body>   \
        </soap:Envelope>';

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_vti_bin/WebPartPages.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: completeFn,
        error: errorFn,
        contentType: "text/xml; charset=\"utf-8\""
    });
}                


$(function() {
    getWebPartProperties('/SitePages/DevHome.aspx',
        function(xhr,textStatus){
            $(xhr.responseXML).find("WebPart").each(function() {
                var wpId = $(this).attr("ID");

                //Manipulate web part by Id via CSOM here... 

            });

        },
        function(xhr,textStatus,errorThrown){
            console.log(errorThrown);
        });
});
1

Using the WebPartManager one could retrieve all web parts in a page, see example by http://blogs.technet.com/b/speschka/archive/2009/11/01/using-the-sharepoint-2010-client-object-model-part-6.aspx

In terms of web service access, there is of course a SOAP web service the Web Part pages web service (http://msdn.microsoft.com/en-us/library/ms774569(v=office.12).aspx) which allows manipulating web parts in a page.

Each web part has an Unique Identifier automatically added by SharePoint anyway, so you don't need to add another one. Your best choice here is still the WebPartManager (Shared or Personal).

  • What properties can you get via WebPartManager which could be used as identifier? – thomius Jan 2 '14 at 10:33
1
//get the connection
ClientContext ctx = new ClientContext("http://foo");


//get the home page
File home = ctx.Web.GetFileByServerRelativeUrl("/SitePages/home.aspx");


//get the web part manager
LimitedWebPartManager wpm = home.GetLimitedWebPartManager(PersonalizationScope.Shared);

//load the web part definitions
ctx.Load(wpm.WebParts);
ctx.ExecuteQuery();

//get the web part definition
WebPartDefinition wpd = wpm.WebParts.GetById(MyWebPartId);

//set the title
wpd.WebPart.Title = "My Web Part Title";

the above does what your asking for as c. marius has pointed out, as for a unique ID, its already set. wpd.WebPart.ID will show the id.... if you return that value within a lable or textbox than youll see the unique ID that is auto generated! Title and description are not unique!

http://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.client.webparts.webpartdefinition_members.aspx

if you want to search by title:

//get the connection
ClientContext ctx = new ClientContext("http://foo");


//get the home page
File home = ctx.Web.GetFileByServerRelativeUrl("/SitePages/home.aspx");


//get the web part manager
LimitedWebPartManager wpm = home.GetLimitedWebPartManager(PersonalizationScope.Shared);

//load the web part definitions
ctx.Load(wpm.WebParts);
ctx.ExecuteQuery();

foreach ( WebPart wp in wpm.WebParts)
{
       //get the web part definition
       WebPartDefinition wpd = wp;

      //get the title
      string title = wpd.WebPart.Title;

      //get the ID
      string id = wpd.WebPart.ID;

      //get a specific webpart with a specific title
      if (wpd.WebPart.Title == "HOME")
      {
         //return ID
         string id = wpd.WebPart.ID;
      }

}
  • I meant that we need to store externally id information to the web part and cannot use the existing ID... However, I just solved this by using WebPartPages.asmx and GetWebPartProperties2 – thomius Jan 2 '14 at 11:12
  • I don't see the ID property of the WebPart object msdn.microsoft.com/en-us/library/… am I missing something here? – GavinB Jun 12 '14 at 5:32
  • @GavinB this is the correct place! msdn.microsoft.com/en-us/library/… – Ali Jafer Jun 12 '14 at 7:01
  • Thanks @aliSharepoint, that means your code should read: string id = wpd.Id; and not string id = wpd.WebPart.ID; though I think as the Id property is on the WebPartDefinition and not the WebPart – GavinB Jun 12 '14 at 10:49

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.