سؤال

كيفية إضافة جزء ويب على الصفحة بطريقة يمكن أن نجدها لاحقا (باستخدام CSOM) - هل يمكننا تخزين معلومات معرف مخزن بطريقة أو بأخرى إلى جزء ويب؟قل يمكن للمستخدم تغيير العنوان والوصف (حتى لا يمكن استخدامها كمعرفات).

أو هل لدينا لاستخدام خدمات الويب (ASMX) ..

تحرير

يظهر ضروري ASMX. فاديم يقترح حل جافا سكريبت أدناه. هنا أيضا رمز جانب خادم بسيط (يستخدم العميل _vti_bin / webpartpages.asmx) giveacodicetagpre.

edit2 نظرا لأن نقاط علي أدناه يمكننا استخدام جزء ويب 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