不確定從哪裏開始。我有代碼設置來創建一個新站點,然後從模板站點複製外觀,列表和Web部件。SharePoint - 將網站部件從模板網站複製到新網站 - XML錯誤
它在作爲系統管理員登錄時工作得很好!
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的 作家,XmlSerializer的個XML,布爾shouldSerializeAll) .SharePoint.WebPartPages.WebPart.WriteXmlAsBytes(XmlSerializer的 個XML)在 Microsoft.SharePoint.WebPartPages.WebPart.WriteXmlGlobal(布爾 disableSafeControlsCheck)在 Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(PersonalizationScope 範圍)在 Microsoft.SharePoint程序.WebPartPages.BinaryWebPartSerializer.get_Links() at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartToStore(Web部件 web部件,的Int32 viewId,字符串viewGuid)在 Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart 超集,布爾throwIfLocked)在 Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager。 AddWebPartInternal(Web部件 web部件,字符串了zoneid,的Int32 ZONEINDEX,布爾throwIfLocked)在 Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPart(Web部件 web部件,字符串了zoneid,的Int32 ZONEINDEX)在 admSitePanelSolution.admSiteCreateCollection。 <> C_ DisplayClasse.b _c()
而的InnerException { 「訪問被拒絕(從HRESULT異常:0X80070005(E_ACCESSDENIED))。」}:在 Microsoft.SharePoint程序
.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException EX)在 Microsoft.SharePoint.Library.SPRequest.GetViewsSchemaXml(字符串 bstrUrl,字符串bstrListName,布爾bFullBlown,ISP2DSafeArrayWriter p2DWriter,的Int32 & plDefaultViewIndex)在 Microsoft.SharePoint.SPViewCollection.EnsureViewSchema(布爾 fullBlownSchema)維持在 Microsoft.SharePoint.SPList.get_Views()在 Microsoft.SharePoint.SPList Microsoft.SharePoint.SPViewCollection..ctor(SPList列表)。 get_DefaultView() Microsoft.SharePoint.SPList.get_DefaultViewUrl()at Microsoft.SharePoint.WebPartPages.ListViewWebPart。ShouldSerializeDetailLink() 在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterListViewWebPart.Write9_ListViewWebPart(字符串 N,串NS,ListViewWebPart O,布爾ISNULLABLE,布爾needType) 在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterListViewWebPart.Write10_WebPart(對象 o)
任何人都可以提供一些幫助,爲什麼發生這種情況時沒有以系統管理員身份登錄。整個模塊也在提升的權限內運行,並且SPSite和SPWeb也在高架模塊內實例化。此代碼是摘錄。
UPDATE
如果我讓用戶基礎的網站集的網站集所有者,那麼上面的代碼運行正常。那麼上面代碼中的內容會重置上下文,或者需要引用基本網站集。謝謝。