如何:使用网页上的 Web 部件
上次修改时间: 2011年4月30日
适用范围: SharePoint Foundation 2010
本文内容
更新 Web 部件的标题
向页面中添加 Web 部件
从页面中删除 Web 部件
使用 Microsoft.SharePoint.Client.WebParts 命名空间 (JavaScript: SP.WebParts) 中的类可通过客户端对象模型来处理 Web 部件。LimitedWebPartManager 类 (JavaScript: LimitedWebPartManager) 允许您通过 WebParts 属性 (JavaScript: webParts) 访问共享或个人范围内 SharePoint 页上的 Web 部件集合。
更新 Web 部件的标题
以下示例更改指定网站的 Default.aspx 页上 Web 部件集合中的第二个 Web 部件的标题。该示例使用 LINQ 查询表达式以便只返回每个 Web 部件的标题,并调用 SaveWebPartChanges() 方法 (JavaScript: saveWebPartChanges()) 来保存更改。将调用两次 ExecuteQuery() 或 ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) 方法 (JavaScript: executeQueryAsync(succeededCallback, failedCallback)),第一次调用返回 Web 部件集合以便代码可以验证页面上存在 Web 部件,第二次调用使更改在数据库中生效。
using System;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;
namespace SampleCode
{
class UpdateWebPartTitle
{
static void Main()
{
ClientContext oClientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
File oFile = oClientContext.Web.GetFileByServerRelativeUrl("Default.aspx");
LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
oClientContext.Load(limitedWebPartManager.WebParts,
wps => wps.Include(
wp => wp.WebPart.Title));
oClientContext.ExecuteQuery();
if (limitedWebPartManager.WebParts.Count == 0)
{
throw new Exception("No Web Parts on this page.");
}
WebPartDefinition oWebPartDefinition = limitedWebPartManager.WebParts[1];
WebPart oWebPart = oWebPartDefinition.WebPart;
oWebPart.Title = "My New Web Part Title";
oWebPartDefinition.SaveWebPartChanges();
oClientContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Imports Microsoft.SharePoint.Client.WebParts
Namespace Microsoft.SDK.SharePointServices.Samples
Class UpdateWebPartTitle
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim serverRelativeUrl As String = "/sites/MySiteCollection/Default.aspx"
Dim oClientContext As New ClientContext(siteUrl)
Dim oFile As File = oClientContext.Web.GetFileByServerRelativeUrl(serverRelativeUrl)
Dim limitedWebPartManager As LimitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared)
oClientContext.Load(limitedWebPartManager.WebParts, _
Function(wps) wps.Include( _
Function(wp) wp.WebPart.Title))
oClientContext.ExecuteQuery()
If limitedWebPartManager.WebParts.Count = 0 Then
Throw New Exception("No Web Parts on this page.")
End If
Dim oWebPartDefinition As WebPartDefinition = limitedWebPartManager.WebParts(1)
Dim oWebPart As WebPart = oWebPartDefinition.WebPart
oWebPart.Title = "My New Web Part Title"
oWebPartDefinition.SaveWebPartChanges()
oClientContext.ExecuteQuery()
End Sub
End Class
End Namespace
siteUrl = '/sites/MySiteCollection';
serverRelativeUrl = '/sites/MySiteCollection/Default.aspx';
function updateWebPartTitle() {
this.clientContext = new SP.ClientContext(siteUrl);
var oFile = clientContext.get_web().getFileByServerRelativeUrl(serverRelativeUrl);
var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
this.collWebPart = limitedWebPartManager.get_webParts();
clientContext.load(collWebPart);
clientContext.executeQueryAsync(Function.createDelegate(this, this.changeTitle), Function.createDelegate(this, this.onQueryFailed));
}
function changeTitle() {
if (!collWebPart.get_count()) {
alert('No Web Parts on this page.');
}
var oWebPartDefinition = collWebPart.get_item(2);
this.oWebPart = oWebPartDefinition.get_webPart();
oWebPart.set_title('My New Web Part Title');
oWebPartDefinition.saveWebPartChanges();
clientContext.load(oWebPart, 'TitleUrl');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert('Title changed for Web Part: ' + this.oWebPart.get_titleUrl());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
向页面中添加 Web 部件
以下示例添加一个自定义内容编辑器 Web 部件,使其按顺序成为指定网站的 Default.aspx 页左侧区域内的第二个 Web 部件。该示例定义该 Web 部件的 XML,将该字符串传递给 ImportWebPart(String) 方法 (JavaScript: importWebPart(webPartXml)),然后调用 AddWebPart(WebPart, String, Int32) 方法 (JavaScript: addWebPart(webPart, zoneId, zoneIndex)) 以便将 Web 部件添加到页面中。
using System;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;
namespace SampleCode
{
class AddWebPart
{
static void Main()
{
ClientContext oClientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
File oFile = oClientContext.Web.GetFileByServerRelativeUrl("Default.aspx");
LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
string xmlWebPart = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<WebPart xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
" xmlns=\"https://schemas.microsoft.com/WebPart/v2\">" +
"<Title>My Web Part</Title><FrameType>Default</FrameType>" +
"<Description>Use for formatted text, tables, and images.</Description>" +
"<IsIncluded>true</IsIncluded><ZoneID></ZoneID><PartOrder>0</PartOrder>" +
"<FrameState>Normal</FrameState><Height /><Width /><AllowRemove>true</AllowRemove>" +
"<AllowZoneChange>true</AllowZoneChange><AllowMinimize>true</AllowMinimize>" +
"<AllowConnect>true</AllowConnect><AllowEdit>true</AllowEdit>" +
"<AllowHide>true</AllowHide><IsVisible>true</IsVisible><DetailLink /><HelpLink />" +
"<HelpMode>Modeless</HelpMode><Dir>Default</Dir><PartImageSmall />" +
"<MissingAssembly>Cannot import this Web Part.</MissingAssembly>" +
"<PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge><IsIncludedFilter />" +
"<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, " +
"PublicKeyToken=94de0004b6e3fcc5</Assembly>" +
"<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>" +
"<ContentLink xmlns=\"https://schemas.microsoft.com/WebPart/v2/ContentEditor\" />" +
"<Content xmlns=\"https://schemas.microsoft.com/WebPart/v2/ContentEditor\">" +
"<![CDATA[This is a first paragraph!<DIV> </DIV>And this is a second paragraph.]]></Content>" +
"<PartStorage xmlns=\"https://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>";
WebPartDefinition oWebPartDefinition = limitedWebPartManager.ImportWebPart(xmlWebPart);
limitedWebPartManager.AddWebPart(oWebPartDefinition.WebPart, "Left", 1);
oClientContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Imports Microsoft.SharePoint.Client.WebParts
Namespace Microsoft.SDK.SharePointServices.Samples
Class AddWebPart
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim serverRelativeUrl As String = "/sites/MySiteCollection/Default.aspx"
Dim oClientContext As New ClientContext(siteUrl)
Dim oFile As File = oClientContext.Web.GetFileByServerRelativeUrl(serverRelativeUrl)
Dim limitedWebPartManager As LimitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared)
Dim xmlWebPart As String = "<?xml version='1.0' encoding='utf-8'?>" + _
"<WebPart xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" + _
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'" + _
" xmlns='https://schemas.microsoft.com/WebPart/v2'>" + _
"<Title>My Web Part</Title><FrameType>Default</FrameType>" + _
"<Description>Use for formatted text, tables, and images.</Description>" + _
"<IsIncluded>true</IsIncluded><ZoneID></ZoneID><PartOrder>0</PartOrder>" + _
"<FrameState>Normal</FrameState><Height /><Width /><AllowRemove>true</AllowRemove>" + _
"<AllowZoneChange>true</AllowZoneChange><AllowMinimize>true</AllowMinimize>" + _
"<AllowConnect>true</AllowConnect><AllowEdit>true</AllowEdit>" + _
"<AllowHide>true</AllowHide><IsVisible>true</IsVisible><DetailLink /><HelpLink />" + _
"<HelpMode>Modeless</HelpMode><Dir>Default</Dir><PartImageSmall />" + _
"<MissingAssembly>Cannot import this Web Part.</MissingAssembly>" + _
"<PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge><IsIncludedFilter />" + _
"<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, " + _
"PublicKeyToken=94de0004b6e3fcc5</Assembly>" + _
"<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>" + _
"<ContentLink xmlns='https://schemas.microsoft.com/WebPart/v2/ContentEditor' />" + _
"<Content xmlns='https://schemas.microsoft.com/WebPart/v2/ContentEditor'>" + _
"<![CDATA[This is a first paragraph!<DIV> </DIV>And this is a second paragraph.]]></Content>" + _
"<PartStorage xmlns='https://schemas.microsoft.com/WebPart/v2/ContentEditor' /></WebPart>"
Dim oWebPartDefinition As WebPartDefinition = limitedWebPartManager.ImportWebPart(webPartXml)
limitedWebPartManager.AddWebPart(oWebPartDefinition.WebPart, "Left", 1)
oClientContext.ExecuteQuery()
End Sub
End Class
End Namespace
siteUrl = '/sites/MySiteCollection';
serverRelativeUrl = '/sites/MySiteCollection/Default.aspx';
function addWebPart() {
var clientContext = new SP.ClientContext(siteUrl);
var oFile = clientContext.get_web().getFileByServerRelativeUrl(serverRelativeUrl);
var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var webPartXml = '<?xml version=\"1.0\" encoding=\"utf-8\"?>' +
'<WebPart xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"' +
' xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"' +
' xmlns=\"https://schemas.microsoft.com/WebPart/v2\">' +
'<Title>My Web Part</Title><FrameType>Default</FrameType>' +
'<Description>Use for formatted text, tables, and images.</Description>' +
'<IsIncluded>true</IsIncluded><ZoneID></ZoneID><PartOrder>0</PartOrder>' +
'<FrameState>Normal</FrameState><Height /><Width /><AllowRemove>true</AllowRemove>' +
'<AllowZoneChange>true</AllowZoneChange><AllowMinimize>true</AllowMinimize>' +
'<AllowConnect>true</AllowConnect><AllowEdit>true</AllowEdit>' +
'<AllowHide>true</AllowHide><IsVisible>true</IsVisible><DetailLink /><HelpLink />' +
'<HelpMode>Modeless</HelpMode><Dir>Default</Dir><PartImageSmall />' +
'<MissingAssembly>Cannot import this Web Part.</MissingAssembly>' +
'<PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge><IsIncludedFilter />' +
'<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, ' +
'PublicKeyToken=94de0004b6e3fcc5</Assembly>' +
'<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>' +
'<ContentLink xmlns=\"https://schemas.microsoft.com/WebPart/v2/ContentEditor\" />' +
'<Content xmlns=\"https://schemas.microsoft.com/WebPart/v2/ContentEditor\">' +
'<![CDATA[This is a first paragraph!<DIV> </DIV>And this is a second paragraph.]]></Content>' +
'<PartStorage xmlns=\"https://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>';
var oWebPartDefinition = limitedWebPartManager.importWebPart(webPartXml);
this.oWebPart = oWebPartDefinition.get_webPart();
limitedWebPartManager.addWebPart(oWebPart, 'Left', 1);
clientContext.load(oWebPart);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert('Web Part added: ' + oWebPart.get_title());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
从页面中删除 Web 部件
以下示例演示了如何使用 DeleteWebPart() 方法 (JavaScript: deleteWebPart()) 从指定网站的 Home.aspx 页中删除第一个 Web 部件。
using System;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;
namespace SampleCode
{
class DeleteWebPart
{
static void Main()
{
ClientContext oClientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
File oFile = oClientContext.Web.GetFileByServerRelativeUrl("/sites/MySiteCollection/SitePages/Home.aspx ");
LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
oClientContext.Load(limitedWebPartManager.WebParts);
oClientContext.ExecuteQuery();
if (limitedWebPartManager.WebParts.Count == 0)
{
throw new Exception("No Web Parts to delete.");
}
WebPartDefinition webPartDefinition = limitedWebPartManager.WebParts[0];
webPartDefinition.DeleteWebPart();
oClientContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Imports Microsoft.SharePoint.Client.WebParts
Namespace Microsoft.SDK.SharePointServices.Samples
Class DeleteWebPart
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim serverRelativeUrl As String = "/sites/MySiteCollection/SitePages/Home.aspx"
Dim oClientContext As New ClientContext(siteUrl)
Dim oFile As File = oClientContext.Web.GetFileByServerRelativeUrl(serverRelativeUrl)
Dim limitedWebPartManager As LimitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared)
oClientContext.Load(limitedWebPartManager.WebParts)
oClientContext.ExecuteQuery()
If limitedWebPartManager.WebParts.Count = 0 Then
Throw New Exception("No Web Parts to delete.")
End If
Dim webPartDefinition As WebPartDefinition = limitedWebPartManager.WebParts(0)
webPartDefinition.DeleteWebPart()
oClientContext.ExecuteQuery()
End Sub
End Class
End Namespace
siteUrl = '/sites/MySiteCollection';
serverRelativeUrl = '/sites/MySiteCollection/SitePages/Home.aspx';
function retrieveWebParts() {
this.clientContext = new SP.ClientContext(siteUrl);
var oFile = clientContext.get_web().getFileByServerRelativeUrl(server2RelativeUrl);
this.limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
this.collWebPart = limitedWebPartManager.get_webParts();
clientContext.load(collWebPart);
clientContext.executeQueryAsync(Function.createDelegate(this, this.deleteWebPart), Function.createDelegate(this, this.onQueryFailed));
}
function deleteWebPart () {
if (!collWebPart.get_count()) {
alert('No Web Parts to delete.');
}
var webPartDefinition = limitedWebPartManager.get_webParts().get_item(0);
webPartDefinition.deleteWebPart();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert('Web Part deleted.');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
有关在 Microsoft SharePoint Foundation 2010 Silverlight 对象模型上下文中使用客户端对象的信息和示例,请参阅使用 Silverlight 对象模型。
请参阅
概念
SharePoint Foundation 中的 Web 部件