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

时间:2012-01-18 10:46:57

标签: c# xml sharepoint web-parts access-denied

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

  

当系统管理员登录时,它的工作效果很好!

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名称空间,String   encodingStyle,String id)at   System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,   对象o)at   Microsoft.SharePoint.WebPartPages.WebPart.SerializeXml(XmlWriter的   writer,XmlSerializer xmls,Boolean shouldSerializeAll)at   Microsoft.SharePoint.WebPartPages.WebPart.WriteXmlAsBytes(XmlSerializer的   xmls)at   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,String viewGuid)at   Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart   超集,布尔throwIfLocked)at   Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPartInternal(的WebPart   webPart,String zoneId,Int32 zoneIndex,Boolean throwIfLocked)at   Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPart(的WebPart   webPart,String zoneId,Int32 zoneIndex)at   admSitePanelSolution.admSiteCreateCollection&LT;&GT; C_ DisplayClasse.b _c()

InnerException {“Access被拒绝。(HRESULT异常:0x80070005(E_ACCESSDENIED))”}:

  

在   Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException   ex)at   Microsoft.SharePoint.Library.SPRequest.GetViewsSchemaXml(字符串   bstrUrl,String bstrListName,Boolean bFullBlown,ISP2DSafeArrayWriter   p2DWriter,Int32&amp; plDefaultViewIndex)at   Microsoft.SharePoint.SPViewCollection.EnsureViewSchema(布尔   fullBlownSchema)at   Microsoft.SharePoint.SPViewCollection..ctor(SPList list)at   Microsoft.SharePoint.SPList.get_Views()at   Microsoft.SharePoint.SPList.get_DefaultView()at   Microsoft.SharePoint.SPList.get_DefaultViewUrl()at   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(对象   ○)

当没有以系统管理员身份登录时,任何人都可以提供一些帮助。整个块也在提升的权限内运行,SPSite和SPWeb也在提升的块内实例化。此代码摘录。

  

更新

如果我让用户成为基本网站集的网站集所有者,那么上面的代码运行正常。那么上面的代码中重复上下文或需要引用基本网站集。谢谢。

1 个答案:

答案 0 :(得分:0)

我的同事为了挽救这一天而猛扑过去!在SP dev中,有一个众所周知的'hack',当权限变得怪异和愚蠢时,它会被大量使用。它是使HttpContext无效。我在线路和循环周围尝试了这个问题但是看起来你必须在导入过程中将它放在代码中更高的位置。

以下是新修补程序。

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

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