none
powershell script to add the existing MMS field( from Web fields) to list RRS feed

  • Question

  • Hi Everyone,

    Please let me know how to add the existing MMS fields from web to list using powershell

    Thanks


    Srinivas



    Wednesday, February 22, 2017 3:41 PM

All replies

  • Wednesday, February 22, 2017 4:52 PM
  • Hi Mohi,

    Thanks for your reply, actually I would like to fetch the MMS Fields from web and add them to Library

    above article is not a working for me. please suggest any other article.

    Thanks


    Srinivas


    Wednesday, February 22, 2017 5:05 PM
  • Hi Srinivas,

    Can you please try the below powershell. First I am adding the managed metadata field to site columns and then adding that field to list. And i am using the site level taxonomy;

    Add-PSSnapin "Microsoft.SharePoint.PowerShell"

    function Create-TaxonomyFieldSiteColumn($taxonomySite, $displayName, $staticName,$fieldGroup, $spWeb)
    {
      $taxonomySession = Get-SPTaxonomySession -site $taxonomySite
      $termStore = $taxonomySession.TermStores["NameOfYourManagedMetadaService"]
      write-host "Connection made with term store -"$termStore.Name
      $taxonomySession = $spWeb.site;

      $termStoreGroup = $termStore.Groups["Testing Dev Term Sets"];
      $termSet = $termStoreGroup.TermSets["TermSet"];
      $taxonomyField = $spWeb.Fields.CreateNewField("TaxonomyFieldType", $displayName);
      $taxonomyField.SspId = $termSet.TermStore.Id
      $taxonomyField.TermSetId = $termSet.Id
      $taxonomyField.AllowMultipleValues = $false
      $taxonomyField.Group = $fieldGroup
      $taxonomyField.StaticName = $staticName
      $taxonomyField.ShowInEditForm = $true
      $taxonomyField.ShowInNewForm = $true
      $taxonomyField.Hidden = $false
      $taxonomyField.Required = $false

      $spWeb.Fields.Add($taxonomyField);

      $spWeb.Update();
    }

    $webURL = "Yourweburl"
    $spSite = New-Object Microsoft.SharePoint.SPSite($webURL)
    $spWeb = $spSite.OpenWeb()
    $listTemplate = $spWeb.ListTemplates["Custom List"];
    $spweb.Lists.Add("List12","Data information DTU",$listTemplate)

    write-host "List added in the Web : " $spweb -foregroundcolor Yellow

    $spList = $spWeb.Lists["List12"]
    $spList.Fields.Add("Publication Date","DateTime",1)
    $spList.Fields.Add("Headline","Text",1)
    $spList.Fields.Add("LeadParagraph","Text",1)
    $spList.Fields.Add("FullText","Text",1)
    $spList.Fields.Add("ContactName","Text",1)
    $spList.Fields.Add("ModifiedTest","DateTime",1)

    $taxonomySite = Get-SPSite $webURL

    $managedMetadataField = Create-TaxonomyFieldSiteColumn $taxonomySite "ManagedMetadatFieldTest" "ManagedMetadatFieldTest" "Custom Columns" $spWeb

    $spList.Fields.Add($spWeb.Fields["ManagedMetadatFieldTest"])

    $spList.Update()
    $spWeb.Dispose()

    Thanks


    K Mohit

    Wednesday, February 22, 2017 6:17 PM
  • Hi Mohi,
    Thanks for your reply, I am written like this but its not working showing error while add the filed to list

    $file=@('sKeyword','Global')  #these are static names of MMS Fileds

    function AddColumn($list)
    {

    for ($i=0; $i -lt $file.length; $i++)
    {
                    try{
                                write-host $file[$i].ToString()
                                $a=$file[$i].ToString()
                                $column1=$list.Fields.TryGetFieldByStaticName($a)

                                #$f1= $List.Fields.GetFieldByInternalName($a) 

                                if($column1-ne $null)
                                {
                                Write-Host "Column $($column1) availble in list";
                                }                      
                                 else
                                {
                                  Write-Host "not availble"
                                  $f=$web.Fields.TryGetFieldByStaticName($a)
                                  Write-Host $f.'Field Name'  
    $List.Fields.Add("TaxonomyFieldType", $f.'Field Name')      #Failing here

                                  $List.Update()
                                  Write-Host "column added success fulle"
                                }
                            }
              catch{
                         $ErrorMessage = $_.Exception.Message
                        write-host $ErrorMessage
                    }
            }
     }

    Thanks


    Srinivas

    Thursday, February 23, 2017 2:06 AM
  • Hi,

    You could try with below code.

    $webURL = "http://sp:12001"
    $spSite = New-Object Microsoft.SharePoint.SPSite($webURL)
    $spWeb = $spSite.OpenWeb()
    $spList = $spWeb.Lists["myDoc"]
    $f=$spWeb.Fields["MyMMField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
    $spList.Fields.Add($f)
    $spList.Update()
    $spWeb.Dispose()

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, February 24, 2017 9:32 AM
  • Hi Lee,

    Thanks for your reply, but $f=$spWeb.Fields["MyMMField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField] is not working for me, getting null/blank value.

    let me know any alternative solution

    Thanks


    Srinivas

    Monday, February 27, 2017 9:57 AM
  • Hi,

    You need replace the “MyMMField” to your managed metadata column name, this is my test field name.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, February 27, 2017 10:10 AM
  • Hi Lee,

    Thanks for the update, but I know its sample MMS Name you have given and I have changed the line with my mms column name but its not getting the value, I would like tell its custom content type. it might be issue any thoughts.

    Thanks


    Srinivas

    Monday, February 27, 2017 3:27 PM
  • Hi,

    If you want to reuse a column, you should create your custom site columns by site settings->Site Columns.

    You could check below thread which may helps you understand the scenarios.

    https://en.share-gate.com/blog/sharepoint-columns-site-columns-or-content-types

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, February 28, 2017 8:04 AM
  • Hi Lee,

    Thanks for you reply, it working when I have done changed as follows

    $taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$web.Site.RootWeb.Fields[$siteCol]

    Thanks


    Srinivas

    Tuesday, February 28, 2017 10:11 AM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can mark the helpful post as answer to help other community members find the helpful information quickly.

    You can also mark your own solution as answer, and we can learn from each other.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, March 8, 2017 9:12 AM