0

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

我使用 C# 做到了这一点。

为了获得所有 Sharepoint Managed 属性,我做了这个:

 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 的属性以及如何设置?

4

1 回答 1

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();
    }
于 2013-08-08T07:38:45.913 回答