SharePoint - 将 Web 部件从模板网站克隆到新网站 - XML 错误

Posted

技术标签:

【中文标题】SharePoint - 将 Web 部件从模板网站克隆到新网站 - XML 错误【英文标题】:SharePoint - Cloning web parts from a template site to a new site - XML error 【发布时间】:2012-02-13 02:13:31 【问题描述】:

不知道从哪里开始。我有代码设置来创建一个新站点,然后从模板站点复制外观、列表和 Web 部件。

以系统管理员身份登录后运行良好!

foreach (Microsoft.SharePoint.WebPartPages.WebPart webPartTemplate in webPartCollectionTemplate)

  try
  
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
webPartManagerTemplate.ExportWebPart(webPartTemplate, xmlWriter);
xmlWriter.Flush();
memoryStream.Flush();
memoryStream.Position = 0;

XmlTextReader xmlReader = new XmlTextReader(memoryStream);

Microsoft.SharePoint.WebPartPages.ListViewWebPart listViewWebPartTemplate = null;
Microsoft.SharePoint.WebPartPages.ListViewWebPart listViewWebPartTarget = null;
Microsoft.SharePoint.WebPartPages.WebPart webPartTarget = null;

bool webPartIsListViewWebPart = false;

// If it throws an exception, the web part is not a ListViewWebPart
try

  listViewWebPartTemplate = (ListViewWebPart)webPartTemplate;
  webPartIsListViewWebPart = true;

catch  

if (webPartIsListViewWebPart)

  string errorMessage = "";
  listViewWebPartTarget = (Microsoft.SharePoint.WebPartPages.ListViewWebPart)webPartManagerTarget.ImportWebPart(xmlReader, out errorMessage);

  if (errorMessage != null)
    // log it

  // Drop the brackets from the Guid and create a Guid object from the string. Then use it to find the list so we can get the title.
  SPList listTemplate = webTemplate.Lists[new Guid(listViewWebPartTemplate.ListName.ToString().TrimStart('').TrimEnd(''))];
  SPList listTarget = webTarget.Lists[listTemplate.Title];

  listViewWebPartTarget.ListName = listTarget.ID.ToString("B").ToUpper();
  listViewWebPartTarget.WebId = webTarget.ID;
  listViewWebPartTarget.TitleUrl = webPartTemplate.TitleUrl.ToString().Replace(siteTemplate.ServerRelativeUrl, siteTarget.ServerRelativeUrl);
  listViewWebPartTarget.Title = webPartTemplate.Title;
  listViewWebPartTarget.ZoneID = webPartTemplate.ZoneID;

  webPartDictTarget.Add(listViewWebPartTarget.ZoneID + webPartTemplate.ZoneIndex.ToString(), listViewWebPartTarget);

else

  string errorMessage = "";
  webPartTarget = (Microsoft.SharePoint.WebPartPages.WebPart)webPartManagerTarget.ImportWebPart(xmlReader, out errorMessage);

  if (errorMessage != null)
    // log it

  webPartTarget.ChromeType = webPartTemplate.ChromeType;
  webPartTarget.TitleUrl = webPartTemplate.TitleUrl.ToString().Replace(siteTemplate.ServerRelativeUrl, siteTarget.ServerRelativeUrl);
  webPartTarget.Title = webPartTemplate.Title;
  webPartTarget.ZoneID = webPartTemplate.ZoneID;

  webPartDictTarget.Add(webPartTarget.ZoneID + webPartTemplate.ZoneIndex.ToString(), webPartTarget);

  
  catch (Exception ex)
  
  


// Acquire keys and sort them.
List<string> list = new List<string>(webPartDictTarget.Keys);
list.Sort();

// Loop through keys.
int i = 0;
foreach (var key in list)

  try
  
    Microsoft.SharePoint.WebPartPages.WebPart wpTmp = (Microsoft.SharePoint.WebPartPages.WebPart)webPartDictTarget[key];
    webPartManagerTarget.AddWebPart(wpTmp, wpTmp.ZoneID, i);
  
  catch (Exception ex)
  
  
  i++;

以下行引发错误:

webPartManagerTarget.AddWebPart(wpTmp, wpTmp.ZoneID, i);

堆栈跟踪如下“生成 XML 文档时出错。”:

在 System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter、Object o、XmlSerializerNamespaces 命名空间、字符串 encodingStyle,字符串 id)在 System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, 对象 o) 在 Microsoft.SharePoint.WebPartPages.WebPart.SerializeXml(XmlWriter writer, XmlSerializer xmls, Boolean shouldSerializeAll) at Microsoft.SharePoint.WebPartPages.WebPart.WriteXmlAsBytes(XmlSerializer xmls) 在 Microsoft.SharePoint.WebPartPages.WebPart.WriteXmlGlobal(布尔 disableSafeControlsCheck) 在 Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(PersonalizationScope 范围)在 Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.get_Links() 在 Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartToStore(WebPart webPart,Int32 viewId,字符串 viewGuid)在 Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart 超集,布尔 throwIfLocked) 在 Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPartInternal(WebPart webPart,字符串 zoneId,Int32 zoneIndex,布尔 throwIfLocked)在 Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPart(WebPart webPart,字符串 zoneId,Int32 zoneIndex)在 admSitePanelSolution.admSiteCreateCollection.c_DisplayClasse.b_c()

InnerException "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))":

在 Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException 例如)在 Microsoft.SharePoint.Library.SPRequest.GetViewsSchemaXml(字符串 bstrUrl,字符串 bstrListName,布尔 bFullBlown,ISP2DSafeArrayWriter p2DWriter, Int32& plDefaultViewIndex) 在 Microsoft.SharePoint.SPViewCollection.EnsureViewSchema(布尔 fullBlownSchema) 在 Microsoft.SharePoint.SPViewCollection..ctor(SPList 列表)在 Microsoft.SharePoint.SPList.get_Views() 在 Microsoft.SharePoint.SPList.get_DefaultView() 在 Microsoft.SharePoint.SPList.get_DefaultViewUrl() 在 Microsoft.SharePoint.WebPartPages.ListViewWebPart.ShouldSerializeDetailLink() 在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterListViewWebPart.Write9_ListViewWebPart(字符串 n, String ns, ListViewWebPart o, Boolean isNullable, Boolean needType) 在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterListViewWebPart.Write10_WebPart(对象 o)

任何人都可以提供一些帮助,说明为什么在未以系统管理员身份登录时会发生这种情况。这整个块也在提升的权限内运行,并且 SPSite 和 SPWeb 也在提升的块内实例化。这段代码是摘录的。

更新

如果我让用户成为基础网站集的网站集所有者,那么上面的代码运行良好。那么上面代码中的内容会重置上下文或需要引用基本网站集。谢谢。

【问题讨论】:

【参考方案1】:

我的同事突然介入以挽救局面!在 SP 开发中,有一个众所周知的“hack”,当权限变得奇怪和愚蠢时,它会被大量使用。它是使HttpContext无效。我在行和循环周围尝试了这个,这给了我一个问题,但看起来你必须在导入期间将它放在代码中更高的位置。

这是新的修复。

HttpContext currentContext = HttpContext.Current;
HttpContext.Current = null;
listViewWebPartTarget = (Microsoft.SharePoint.WebPartPages.ListViewWebPart)webPartManagerTarget.ImportWebPart(xmlReader, out errorMessage);
HttpContext.Current = currentContext;

此代码现在适用于将 webpart 从一个 apsx 页面复制到另一个页面。希望它可以帮助某人。

【讨论】:

以上是关于SharePoint - 将 Web 部件从模板网站克隆到新网站 - XML 错误的主要内容,如果未能解决你的问题,请参考以下文章

在 Sharepoint 2013 中将用户控件添加到 Web 部件

以编程方式在Sharepoint中实例化Web部件页面

将 HTML 从内部文件添加到 SharePoint 上的 Web 部件

从 SharePoint Online Web 部件调用外部服务

如何在SharePoint中将Web部件从一个站点放到另一个站点?

如何在 SharePoint 中将 Web 部件从一个站点放到另一个站点?