解决方案:使用我在这里找到的代码http://stefan-stanev-sharepoint-blog.blogspot.co.uk/2010/02/listviewwebpart-spview-two-sides-of.html在评论部分。我希望 Stefan 不介意我在这里复制其中的一些内容。
需要注意的重要一点是 wp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper(); 行被注释掉,因为否则它将使用当前默认视图的 baseviewid,即 6,这正是我们不想要的。
代码获取照片和视频列表,然后获取列表使用的所有 Web 部件,当它获取 AllPictures Web 部件时,将其保存以供以后删除,添加新的 listviewwebpart(但在此阶段我们只能添加 baseviewid ),然后删除当前的 web 部件(它似乎只能按此顺序运行)然后获取创建的隐藏视图,有关详细信息,请参阅 stefan 的博客。
然后,对 Program.UseList 的第二次调用将一些视图重置为如果 baseviewid 为 1 时的外观,为此,我查看了 baseviewid 为 1 的另一个站点,并在 XmlDefinition 中签入了共享点设计器以准确查看xml 应该是什么样子。
希望这可以帮助其他人。
public void LibraryCode(string url)
{
Guid hiddenViewGuid = Guid.Empty;
SPList library = null;
string listUrl = "Photos1/Forms/AllPictures.aspx";
Program.UseList(url, listUrl, list =>
{
SPFile file = list.ParentWeb.GetFile(listUrl);
Guid listID = file.ParentFolder.ParentListId;
if (!listID.Equals(Guid.Empty))
{
library = list.ParentWeb.Lists[listID];
if (library.ForceCheckout && file.Level != SPFileLevel.Checkout) file.CheckOut();
}
SPLimitedWebPartManager mngr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
SPLimitedWebPartCollection webparts = mngr.WebParts;
System.Web.UI.WebControls.WebParts.WebPart op = null;
for (int k = 0; k < webparts.Count; k++)
{
//get reference to webpart
op = webparts[k];
//check webpart Title to find webpart whose value is to be changed
if (op.Title.Equals("AllPictures"))
{
break;
}
}
XsltListViewWebPart wp = new XsltListViewWebPart();
wp.ListName = list.ID.ToString("B").ToUpper();
//comment out the line below or the new view gets the same baseviewid as
//wp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
wp.XmlDefinition = "<View BaseViewID='1'/>";
mngr.AddWebPart(wp, "Left", 1);
//remove existing web part otherwise there will be two tables of the data on the same page
mngr.DeleteWebPart(op);
hiddenViewGuid = new Guid(wp.ViewGuid);
});
Program.UseList(url, listUrl, list =>
{
SPView view = list.Views[hiddenViewGuid];
view.Title = "All Pictures";
view.Update();
// load the passed viewSchema in an XmlDocument
XmlDocument doc = new XmlDocument();
doc.LoadXml(view.HtmlSchemaXml);
// set the Name attribute with the ID of the hidden LVP's SPView - we use a simple trick to update the full schema of the hidden view
XmlElement root = doc.DocumentElement;
root.SetAttribute("Name", view.ID.ToString("B").ToUpper());
root.SetAttribute("BaseViewID", "1");
root.SetAttribute("Type", "HTML");
root.SetAttribute("Hidden", "False");
// do some changes to the view schema
XmlElement viewFieldsEl = doc.SelectSingleNode("//ViewFields") as XmlElement;
if (viewFieldsEl != null) { viewFieldsEl.InnerXml = @"<FieldRef Name=""DocIcon""/>
<FieldRef Name=""LinkFilename""/>
<FieldRef Name=""ImageSize""/>
<FieldRef Name=""FileSizeDisplay""/>
<FieldRef Name=""Modified""/>
<FieldRef Name=""RequiredField"" Explicit=""TRUE""/>
<FieldRef Name=""PreviewOnForm"" Explicit=""TRUE""/>
<FieldRef Name=""ImageCreateDate""/>
<FieldRef Name=""Project_x0020_Tags""/>
<FieldRef Name=""_Comments""/>
<FieldRef Name=""Originator""/>"; }
XmlElement javascriptEl = doc.SelectSingleNode("//JSLink") as XmlElement;
if (javascriptEl != null) { javascriptEl.InnerXml = "clienttemplates.js|callout.js"; }
// create a dummy view to hold the new schema
SPView cloneView = new SPView(list, doc);
// a small trick with reflection so that we can update the dummy view
typeof(SPView).GetField("m_bExistsInDatabase", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(cloneView, true);
// this will actually update the hidden SPView associated with the LVP
cloneView.Update();
if (library != null)
{
SPFile file = list.ParentWeb.GetFile(listUrl);
if (library.ForceCheckout) file.CheckIn("");
if (library.EnableMinorVersions) file.Publish("");
if (library.EnableModeration) file.Approve("");
}
});
}