1

I get error message when I click on "Add from existing site content types" I want to add existing content type to this document library. Is there any powershell script for the same. Content type is already added in site but when i edit document properties it does not show 1 content type.

My document library name is "ABC" Content type name is "XYZ"

8
  • What is the error message you are getting?
    – Thomson
    Commented Apr 3, 2014 at 11:49
  • Unexpected error, I checked ULS log but could not get rid of error. Do you have idea how to overcome this?
    – Brishal
    Commented Apr 3, 2014 at 12:05
  • Which version of Sharepoint?
    – Thomson
    Commented Apr 3, 2014 at 12:21
  • sharepoint 2010
    – Brishal
    Commented Apr 3, 2014 at 12:52
  • When you get the error , do you get a correlation id?
    – Thomson
    Commented Apr 5, 2014 at 0:39

3 Answers 3

2

Following PS script should help:

#Get site object and specify name of the library to look for in each site
$site = Get-SPSite http://portal
$lookForList = "ABC"

#Walk through each site and change content types on the list specified
$siteA = $site.OpenWeb("http://portal/sites/A")

    write-host "Checking site:" $siteA.Title

    #Make sure content types are allowed on the list specified
    $docLibrary = $siteA.Lists[$lookForList]

    if ($docLibrary -ne $null)
    {
        $docLibrary.ContentTypesEnabled = $true
        $docLibrary.Update()

        #Add site content types to the list
        $ctToAdd = $siteA.ContentTypes["XYZ"]
        $ct = $docLibrary.ContentTypes.Add($ctToAdd)
        write-host "Content type" $ct.Name "added to list" $docLibrary.Title
        $docLibrary.Update()
    }
    else
    {
        write-host "The list" $lookForList "does not exist in site" $siteA.Title
    }


#Dispose of the site object
$site.Dispose()

But you should check the error you get when you navigate to Add from existing content type.. Get the correlation and check the exact error in ULS logs

9
  • ULS log just giving that there is error while adding existing content type... any solution how to overcome this??
    – Brishal
    Commented Apr 3, 2014 at 11:53
  • In site collection there are many site which has same library name, i don't want to go through each site and library. So can i use below?
    – Brishal
    Commented Apr 3, 2014 at 12:02
  • So you want to add the content type in each library in each site? Commented Apr 3, 2014 at 12:13
  • Updated the script to work for all sites within a site collection Commented Apr 3, 2014 at 12:14
  • No I just want for 1 library, like we have 10 "ABC" library in site collection. I do not want to iterate for each document libray. Just for library "ABC" which is under site A. How to use syntax for open web instead of rootweb?
    – Brishal
    Commented Apr 3, 2014 at 12:19
1

Running Powershell and accessing a Subsite (and not the root web) you have to make a slightly different approach. You need to access the root web content type, and add that one to the subsite document library

$SPSite = Get-SPSite "http://portal"
$lookForList = "Shared Documents"

if ($SPSite -ne $null)
{
    foreach ($SPWeb in $SPSite.AllWebs)
    {
        $docLibrary = $SPWeb.Lists[$lookForList]
        $ctInRootWeb = $SPSite.RootWeb.ContentTypes["Company Document"]

        if($docLibrary.ContentTypes["Company Document"].Name -eq "Company Document")
        {
            Write-Host "   CT exist" -ForegroundColor Yellow
        }
        else
        {
            $ct = $docLibrary.ContentTypes.Add($ctInRootWeb)
            $docLibrary.Update()
            Write-Host "   CT Added" -ForegroundColor Green
        }

        if ($SPWeb -ne $null)
        {
            $SPWeb.Dispose()
        }
    }
}

if ($SPSite -ne $null)
{
    $SPSite.Dispose()
}
0

You need to visit the http://{sitename}/_layouts/mngfield.aspx

{sitename}- yoursitename to see if there are any duplicate fields are there.and delete accordingly . This site should help you for a detailed step Link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.