2013-08-07 41 views
0

我需要使用Powershell + Sharepoint Powershell擴展將新的託管元數據屬性添加到Sharepoint 2013中。如何通過Powershell更新Sharepoint 2013元數據託管屬性的「HasMultipleValues」

我是用C#做的。

爲了讓我做這一切的SharePoint託管屬性:

private static string GetAllSPManagedProperties(string searchApplication) 
     { 
      RunspaceConfiguration config = RunspaceConfiguration.Create(); 
      PSSnapInException OExSnapIn = null; 
      PSSnapInInfo pssnap = config.AddPSSnapIn("Microsoft.SharePoint.PowerShell", out OExSnapIn); 
      //create powershell runspace 
      Runspace cmdlet = RunspaceFactory.CreateRunspace(config); 
      cmdlet.Open(); 
      RunspaceInvoke scriptInvoker = new RunspaceInvoke(cmdlet); 
      // set powershell execution policy to unrestricted 
      scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); 
      // create a pipeline and load it with command object 
      Pipeline pipeline = cmdlet.CreatePipeline(); 
      Command cmd = new Command("Get-SPEnterpriseSearchMetadataManagedProperty"); 
      pipeline.Commands.Add(cmd); 
      CommandParameter cmdParam = new CommandParameter("SearchApplication", searchApplication); 
      cmd.Parameters.Add(cmdParam); 
      //pipeline.Commands.Add("Out-String"); 
      // this will format the output 
      IEnumerable<PSObject> output = pipeline.Invoke(); 
      pipeline.Stop(); 
      cmdlet.Close(); 
      // process each object in the output and append to stringbuilder 
      StringBuilder results = new StringBuilder(); 
      foreach (PSObject obj in output) 
      { 
       var typeNames = obj.TypeNames; 

       var p1 = obj.Properties["ID"].Value;   // "AboutMe" object {string} 
       var p2 = obj.Properties["ManagedType"].Value; // Text object {Microsoft.Office.Server.Search.Administration.ManagedDataType} 
       var p3 = obj.Properties["PID"].Value;   // 26   object {int} 
       var p4 = obj.Properties["Name"].Value;   // "AboutMe" object {string} 
       var p5 = obj.Properties["HasMultipleValues"].Value;   // true object {bool} 
       string managedTypeName = (string)p2.ToString(); 

       results.AppendLine(obj.ToString()); 
      } 
      return results.ToString(); 
     } 

的問題是,我試圖以編程方式設置選擇的託管元數據屬性的這個標誌「HasMultipleValues」。

obj.Properties["HasMultipleValues"].Value = true; 

我不知道該怎麼做。我希望找到PSObject的一些更新方法(由pipeline.Invoke()返回,但不幸沒有發現任何有用的東西。

我的問題是,是否可以設置任何ManagedMetadataProperty的屬性以及如何?

回答

0

它看起來像我找到了解決自己。

我沒有找到如何實現通過設置對象屬性的解決方案,但發現通過腳本同樣可以接受的解決方案的方式。

public static void UpdateSharepointManagedMetadataProperty(string searchApplication, string propertyName, bool HasMultipleValues) 
    { 
     RunspaceConfiguration config = RunspaceConfiguration.Create(); 
     PSSnapInException OExSnapIn = null; 
     PSSnapInInfo pssnap = config.AddPSSnapIn("Microsoft.SharePoint.PowerShell", out OExSnapIn); 
     //create powershell runspace 
     Runspace cmdlet = RunspaceFactory.CreateRunspace(config); 
     cmdlet.Open(); 
     RunspaceInvoke scriptInvoker = new RunspaceInvoke(cmdlet); 

     bool set = HasMultipleValues; 
     string script = ""; 
     if (set) 
     { 
      script = 
       string.Format("$prop = Get-SPEnterpriseSearchMetadataManagedProperty -Identity {0} -SearchApplication \"{1}\" \r\n", propertyName, searchApplication) + 
           "$prop.HasMultipleValues = $true \r\n" + 
           "$prop.Queryable = $true \r\n" + 
           "$prop.Refinable = $true \r\n" + 
           "$prop.Searchable = $true \r\n" + 
           "$prop.Retrievable = $true \r\n" + 
           "$prop.Update() \r\n"; 
     } 
     else 
     { 
      script = 
       string.Format("$prop = Get-SPEnterpriseSearchMetadataManagedProperty -Identity {0} -SearchApplication \"{1}\" \r\n", propertyName, searchApplication) + 
           "$prop.HasMultipleValues = $false \r\n" + 
           "$prop.Queryable = $false \r\n" + 
           "$prop.Refinable = $false \r\n" + 
           "$prop.Searchable = $false \r\n" + 
           "$prop.Retrievable = $false \r\n" + 
           "$prop.Update() \r\n"; 
     } 


     var result = scriptInvoker.Invoke(script); 

     cmdlet.Close(); 
    } 
+0

調用commandlets fr om代碼並不漂亮。您應該查看管理API並使用適當的對象imo。 –

相關問題