문제

나중에 우리가 나중에 찾을 수있는 방식으로 페이지에 웹 파트를 추가하는 방법 (CSOM 사용) - 웹 파트에 식별자 정보를 저장할 수 있습니까?사용자가 제목과 설명을 변경할 수 있습니다 (그래서 식별자로 사용할 수 없으므로)

또는 우리는 (ASMX) 웹 서비스를 사용해야합니다.

편집

가 나타납니다. ASMX가 필요합니다. VADIM은 아래의 JavaScript 솔루션을 제안합니다. 여기에서 간단한 서버 측 코드 (클라이언트는 _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 아래의 Ali 포인트로서 우리는 웹 파트 GUID를 식별자로 사용할 수 있습니다. 웹 파트를 만드는 동안 가져오고 외부 위치에 저장하고 나중에 사용할 수 있습니다.그러나이 경우 우리는이 식별자를 사용할 / 저장할 수 없지만 우리 자신을 제공해야합니다.따라서 유일한 남은 가능성은 ASMX 인 것처럼 보입니다.

도움이 되었습니까?

해결책

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);
        });
});

다른 팁

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).

//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;
      }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top