如何通过Powershell更新Sharepoint 2013元数据托管属性的“HasMultipleValues”

时间:2013-08-07 15:05:19

标签: powershell properties metadata managed sharepoint-2013

我需要使用Powershell + Sharepoint Powershell Extensions将新的托管元数据属性添加到Sharepoint 2013中。

我是用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();
        }

问题是我正在尝试以编程方式设置所选Managed Metadata Property的此标志“HasMultipleValues”。

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

我不知道该怎么做。我希望找到一些PSObject的Update方法(由pipeline.Invoke()返回,但遗憾的是没有找到任何有用的东西。

我的问题是,是否可以设置任何ManagedMetadataProperty的属性以及如何?

1 个答案:

答案 0 :(得分: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();
    }