1

我正在使用 C# 修改我的 sharepoint 2013 农场的照片库中的视图,目前该农场大约有 600 个站点。

我正在使用方法 olist.Views.Add("AllPictures", strColl, sQuery, 40, true, true,Microsoft.SharePoint.SPViewCollection.SPViewType.Html, false)

基本上我正在尝试重新创建主“所有图片”视图,因为运行例程将此视图切换到网格导致该视图不再显示任何内容。我的问题是,一旦我删除了视图并运行上面的代码,它将创建一个新的 All Pictures 视图,其 baseviewid 为 6,因此照片显示为缩略图而不是包含照片详细信息的表格。我需要能够将 baseviewid 指定为 1,但我还没有找到一种方法来做到这一点。

4

1 回答 1

0

解决方案:使用我在这里找到的代码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("");
            }
        });
    }
于 2013-10-15T10:34:50.057 回答