Share via


Per location view settings using SharePoint Object Model

You have to set the library.RootFolder.Properties["client_MOSS_MetadataNavigationSettings"] property to get change the per location view settings using the SharePoint Object Model.

    1: using (SPSite site = new SPSite("https://blre3r15-4"))
    2:             {
    3:                 //TaxonomySession session = new TaxonomySession(site);
    4:                 using (SPWeb web = site.OpenWeb())
    5:                 {
    6:                     SPList library = web.Lists["shared documents"];
    7:                     SPContentTypeCollection lsl = library.ContentTypes;
    8:                     foreach (SPContentType contenttype in lsl)
    9:                     {
   10:                         Console.WriteLine(contenttype.Id.ToString());
   11:                     }
   12:  
   13:                     foreach (SPView view in library.Views)
   14:                     {
   15:                         Console.WriteLine(view.ID.ToString());
   16:                     }
   17:                     System.Xml.XmlDocument document = new System.Xml.XmlDocument();
   18:                     //document.Load("c:\\cases\\Settings.xml");
   19:                     document.LoadXml(library.RootFolder.Properties["client_MOSS_MetadataNavigationSettings"].ToString());
   20:                     System.IO.StreamWriter writer = new System.IO.StreamWriter("c:\\cases\\Settings.xml");
   21:                     writer.Write(library.RootFolder.Properties["client_MOSS_MetadataNavigationSettings"].ToString());
   22:                     writer.Close();
   23:                     //library.RootFolder.Properties["client_MOSS_MetadataNavigationSettings"] = document.OuterXml.ToString();
   24:                     //library.RootFolder.Update();
   25:                     //library.Update();
   26:                     //web.Update();
   27:                     Console.WriteLine(library.RootFolder.Properties["client_MOSS_MetadataNavigationSettings"].ToString());
   28:                     SPViewCollection collection = library.Views;
   29:                     
   30:                     //MetadataNavigationSettings mdnSettings = MetadataNavigationSettings.GetMetadataNavigationSettings(library);
   31:                     //mdnSettings.SettingsXml = document.OuterXml.ToString();
   32:                     //MetadataNavigationSettings.SetMetadataNavigationSettings(library, mdnSettings);
   33:                     //Console.WriteLine(mdnSettings.SettingsXml.ToString());
   34:                     Console.ReadKey();
   35:                                         
   36:                 }
   37:             }
    1: <MetadataNavigationSettings SchemaVersion="1" IsEnabled="True" AutoIndex="True"><NavigationHierarchies><FolderHierarchy HideFoldersNode="False" />
    2: <MetadataField FieldID="03e45e84-1992-4d42-9116-26f756012634" FieldType="ContentTypeId" CachedName="ContentTypeId" CachedDisplayName="Content Type">
    3:   <ViewSettings UniqueNodeId="0x01010070FC6B859DF4CD4FAEDD269B75BE2C8E003443C1064EC2D9489B7D978018AFFEF3">
    4:     <View ViewId="b0e7c6b8-7a05-4943-8323-bc7d1ab90c4a" CachedName="Test2" Index="2" CachedUrl="Shared Documents/Forms/Test2.aspx" />
    5:     <View ViewId="2017c43f-7b0d-4580-9d9d-d3c699b51b6a" CachedName="All Documents" Index="1" CachedUrl="Shared Documents/Forms/AllItems.aspx" />
    6:     <View ViewId="82185a44-dd31-4a12-91bf-da6798ff2c26" CachedName="Test1" Index="0" CachedUrl="Shared Documents/Forms/Test1.aspx" />
    7:     <View ViewId="c1bd6b7b-ba53-4b0f-984d-3c24886dd4f5" CachedName="Merge Documents" Index="-1" CachedUrl="Shared Documents/Forms/Combine.aspx" />
    8:     <View ViewId="403a91bb-2f07-4eef-bcaa-f14778148edd" CachedName="Relink Documents" Index="-2" CachedUrl="Shared Documents/Forms/repair.aspx" />
    9:     <View ViewId="79b33591-e594-4d4d-afd0-fd700a08f480" CachedName="assetLibTemp" Index="-3" CachedUrl="Shared Documents/Forms/Thumbnails.aspx" />
   10:     <View ViewId="d38fef41-7cf5-4a73-adc9-7edb956addfc" CachedName="" Index="-4" CachedUrl="SitePages/Home.aspx" />
   11:   </ViewSettings><ViewSettings UniqueNodeId="0x01010090C01EC2217EB24E9C25C0B20DA04FE0"><View ViewId="82185a44-dd31-4a12-91bf-da6798ff2c26" CachedName="Test1" Index="0" CachedUrl="Shared Documents/Forms/Test1.aspx" /><View ViewId="2017c43f-7b0d-4580-9d9d-d3c699b51b6a" CachedName="All Documents" Index="1" CachedUrl="Shared Documents/Forms/AllItems.aspx" /><View ViewId="b0e7c6b8-7a05-4943-8323-bc7d1ab90c4a" CachedName="Test2" Index="2" CachedUrl="Shared Documents/Forms/Test2.aspx" /><View ViewId="c1bd6b7b-ba53-4b0f-984d-3c24886dd4f5" CachedName="Merge Documents" Index="-1" CachedUrl="Shared Documents/Forms/Combine.aspx" /><View ViewId="403a91bb-2f07-4eef-bcaa-f14778148edd" CachedName="Relink Documents" Index="-2" CachedUrl="Shared Documents/Forms/repair.aspx" /><View ViewId="79b33591-e594-4d4d-afd0-fd700a08f480" CachedName="assetLibTemp" Index="-3" CachedUrl="Shared Documents/Forms/Thumbnails.aspx" /><View ViewId="d38fef41-7cf5-4a73-adc9-7edb956addfc" CachedName="" Index="-4" CachedUrl="SitePages/Home.aspx" /></ViewSettings></MetadataField></NavigationHierarchies><KeyFilters /><ManagedIndices><ManagedIndex IndexID="03e45e84-1992-4d42-9116-26f756012634" IndexFieldName="ContentTypeId" IndexFieldID="03e45e84-1992-4d42-9116-26f756012634" /></ManagedIndices><ViewSettings UniqueNodeId=""><View ViewId="2017c43f-7b0d-4580-9d9d-d3c699b51b6a" CachedName="All Documents" Index="0" CachedUrl="Shared Documents/Forms/AllItems.aspx" /><View ViewId="82185a44-dd31-4a12-91bf-da6798ff2c26" CachedName="Test1" Index="4" CachedUrl="Shared Documents/Forms/Test1.aspx" /><View ViewId="b0e7c6b8-7a05-4943-8323-bc7d1ab90c4a" CachedName="Test2" Index="5" CachedUrl="Shared Documents/Forms/Test2.aspx" /></ViewSettings></MetadataNavigationSettings>
    1: using System;
    2: using System.Collections.Generic;
    3: using System.ComponentModel;
    4: using System.Data;
    5: using System.Drawing;
    6: using System.Linq;
    7: using System.Text;
    8: using System.Windows.Forms;
    9: using Microsoft.SharePoint;
   10: using Microsoft.Office.Server;
   11: using Microsoft.Office.DocumentManagement.MetadataNavigation;
   12: using System.Xml;
   13:  
   14: namespace MetaDataViewAssosiaction
   15: {
   16:     public partial class Form1 : Form
   17:     {
   18:  
   19:        private const string DefaultListSettingsXmlString =  @"<MetadataNavigationSettings SchemaVersion=""1"" IsEnabled=""true"" AutoIndex=""True""><NavigationHierarchies><FolderHierarchy /></NavigationHierarchies><KeyFilters></KeyFilters></MetadataNavigationSettings>";
   20:  
   21:         
   22:         public Form1()
   23:         {
   24:             InitializeComponent();
   25:         }
   26:         SPSite site;
   27:         private void btnFillData_Click(object sender, EventArgs e)
   28:         {
   29:             string siteURL = textBox1.Text.ToString().Trim();
   30:             site = new SPSite(siteURL);
   31:             FillWebs(site);
   32:            
   33:         }
   34:  
   35:         private void FillWebs(SPSite site)
   36:         {
   37:             foreach (SPWeb web in site.AllWebs)
   38:             {
   39:                 lstWeb.Items.Add(web.Name);
   40:             }
   41:         }
   42:  
   43:         private void FillList()
   44:         {
   45:             using (SPWeb web = site.OpenWeb(this.lstWeb.SelectedItem.ToString()))
   46:             { 
   47:                 foreach(SPList List in web.Lists)
   48:                 {
   49:                     lstList.Items.Add(List.Title);
   50:                 }
   51:                 
   52:             }
   53:  
   54:         }
   55:  
   56:         DataTable table = null;
   57:         private void FillContentTypes()
   58:         {
   59:             table = new DataTable();
   60:             DataColumn IDColumn = new DataColumn("ID");
   61:             IDColumn.DataType = typeof(string);
   62:             table.Columns.Add(IDColumn);
   63:             
   64:             DataColumn NameColumn = new DataColumn("ContentTypeName");
   65:             NameColumn.DataType = typeof(string);
   66:             table.Columns.Add(NameColumn);
   67:             using (SPWeb web = site.OpenWeb(this.lstWeb.SelectedItem.ToString()))
   68:             {
   69:  
   70:                 SPList list = web.Lists[this.lstList.SelectedItem.ToString()];
   71:                 foreach (SPContentType ContentType in list.ContentTypes )
   72:                 {
   73:                     DataRow row = table.NewRow();
   74:                     row["ID"] = ContentType.Id;
   75:                     row["ContentTypeName"] = ContentType.Name;
   76:                     table.Rows.Add(row);
   77:                     
   78:                 }
   79:  
   80:             }
   81:  
   82:             lstContentTypes.DataSource = table;
   83:             lstContentTypes.DisplayMember = table.Columns["ContentTypeName"].ColumnName;
   84:             lstContentTypes.ValueMember = table.Columns["ID"].ColumnName;
   85:             
   86:         }
   87:  
   88:         DataTable ViewTable = null;
   89:         private void FillViews()
   90:         {
   91:             ViewTable = new DataTable();
   92:             DataColumn IDColumn = new DataColumn("ID");
   93:             IDColumn.DataType = typeof(string);
   94:             ViewTable.Columns.Add(IDColumn);
   95:  
   96:             DataColumn NameColumn = new DataColumn("ViewName");
   97:             NameColumn.DataType = typeof(string);
   98:             ViewTable.Columns.Add(NameColumn);
   99:             using (SPWeb web = site.OpenWeb(this.lstWeb.SelectedItem.ToString()))
  100:             {
  101:                 SPList list = web.Lists[this.lstList.SelectedItem.ToString()];
  102:                 foreach (SPView  View in list.Views)
  103:                 {
  104:                     DataRow row = ViewTable.NewRow();
  105:                     row["ID"] = View.ID;
  106:                     row["ViewName"] = View.Title;
  107:                     ViewTable.Rows.Add(row);
  108:                 }
  109:  
  110:             }
  111:  
  112:             lstViews.DataSource = ViewTable;
  113:             lstViews.DisplayMember = ViewTable.Columns["ViewName"].ColumnName;
  114:             lstViews.ValueMember = ViewTable.Columns["ID"].ColumnName;
  115:         }
  116:  
  117:         private void lstWeb_SelectedIndexChanged(object sender, EventArgs e)
  118:         {
  119:             FillList();
  120:         }
  121:  
  122:         private void lstList_SelectedIndexChanged(object sender, EventArgs e)
  123:         {
  124:             FillContentTypes();
  125:             FillViews();
  126:             MessageBox.Show("Done");
  127:  
  128:         }
  129:  
  130:         private void button1_Click(object sender, EventArgs e)
  131:         {
  132:             string XML = GenerateXML();
  133:             MessageBox.Show(XML);
  134:         }
  135:  
  136:         string XmlElementNameRootElement = "MetadataNavigationSettings";
  137:         string XmlAttributeNameSchemaVersion = "SchemaVersion";
  138:         string XmlAttributeNameIsEnabled = "IsEnabled";
  139:         string XmlAttributeNameAutomaticIndexManagement = "AutoIndex";
  140:         string XmlElementNameNavigationHierarchies = "NavigationHierarchies";
  141:         string XmlElementNameKeyFilters = "KeyFilters";
  142:         string XmlElementNameFolderHierarchy = "FolderHierarchy";
  143:         private string  GenerateXML()
  144:         {
  145:             StringBuilder sb = new StringBuilder();
  146:  
  147:             XmlWriterSettings writerSettings = new XmlWriterSettings();
  148:             writerSettings.OmitXmlDeclaration = true;
  149:             writerSettings.Indent = false;
  150:             writerSettings.NewLineHandling = NewLineHandling.None;
  151:  
  152:             XmlWriter writer = XmlWriter.Create(sb, writerSettings);
  153:  
  154:             writer.WriteStartElement(XmlElementNameRootElement);
  155:             writer.WriteAttributeString(XmlAttributeNameSchemaVersion, "1");
  156:             writer.WriteAttributeString(XmlAttributeNameIsEnabled, "True");
  157:             writer.WriteAttributeString(XmlAttributeNameAutomaticIndexManagement, "True");
  158:             // Should we add the cached list Url and ID
  159:  
  160:             //// Serialize Navigation hierarchies collection
  161:             writer.WriteStartElement(XmlElementNameNavigationHierarchies);
  162:             writer.WriteStartElement(XmlElementNameFolderHierarchy);
  163:             WriteStorageXml(writer);
  164:             writer.WriteEndElement();
  165:             writer.WriteEndElement(); // XmlElementNameNavHierarchies
  166:  
  167:             writer.WriteStartElement(XmlElementNameKeyFilters);
  168:             //MetadataNavigationSettings.WriteStorageXml<MetadataNavigationKeyFilter>(this.ConfiguredKeyFilters, writer);
  169:             writer.WriteEndElement(); // XmlElementNameKeyFilters
  170:  
  171:  
  172:             writer.WriteEndElement(); // XmlElementNameRootElement
  173:  
  174:             writer.Flush();
  175:             writer.Close();
  176:  
  177:             return sb.ToString();
  178:           
  179:         }
  180:  
  181:         string xmlElementNameViewSettings = "ViewSettings";
  182:         string XmlAttributeNameUniqueNodeId = "UniqueNodeId";
  183:         private void WriteStorageXml(XmlWriter writer)
  184:         {
  185:            
  186:             writer.WriteStartElement(xmlElementNameViewSettings);
  187:             writer.WriteAttributeString(XmlAttributeNameUniqueNodeId, "");
  188:             writer.WriteEndElement();
  189:         }
  190:  
  191:         private void btnMap_Click(object sender, EventArgs e)
  192:         {
  193:             DataRowView row = (DataRowView)this.lstContentTypes.SelectedItem;
  194:             DataRowView viewRow = (DataRowView)this.lstViews.SelectedItem;
  195:             string strMapping = row.Row["ContentTypeName"].ToString() + ";" +
  196:                                 this.lstContentTypes.SelectedValue.ToString() + ";" +
  197:                                 viewRow.Row["ViewName"].ToString() + ";" +
  198:                                 this.lstViews.SelectedValue.ToString();
  199:             
  200:             this.lstMapping.Items.Add(strMapping);
  201:         }
  202:     }
  203: }

Comments