Spring til indholdet

Tilføj en ContentType til alle dokumenter i en SiteCollection med PowerShell i SharePoint 2013

To PowerShell-scripts, der giver alle dokumentbiblioteker i en site collection en ny ContentType, flytter dokumenterne over på den uden at ændre “modified by” og “modified date” og fjerner ContentTypen “Document”.

PowerShell 5 min. læsning

Jeg stødte på et behov for at ændre alle eksisterende dokumenter i en Site Collection til en anden ContentType, men uden at ændre data som “modified by” eller “modified date”.

Hvorfor?

Jeg har brug for at tilknytte en kolonne til alle dokumenter, og det er bare ikke god praksis at tilføje kolonner til standard-ContentTypen “Document”. Jeg skal tilføje ContentTypen til dokumentbiblioteket, knytte alle dokumenter med ContentTypen “Document” til den nye ContentType og så slette ContentTypen “Document”. Brugerne skal ikke tænke på ContentTypes, og på hvordan man bruger den rigtige, så det her skal køre en gang om dagen for at fange nye dokumentbiblioteker og ændre dokumenterne, uden at brugerne mærker noget.

Planen for hvert dokumentbibliotek:#

  1. Tilføj ContentTypen “MyCustomDocument”
  2. Slå versionering til (majorversioner og ingen grænse)
  3. Knyt alle dokumenter til ContentTypen “MyCustomDocument”
  4. Fjern standard-ContentTypen “Document”
  5. Opret planlagte opgaver til hele processen

Jeg har lavet to PowerShell-scripts for at få det gennemført. Det ene script tager sig af dokumentbiblioteket, og det andet tager sig af alle dokumenterne i de biblioteker. Kører man scriptsene manuelt, skal man køre det første script to gange, da det kun kører, når alle dokumenter i biblioteket har skiftet deres ContentType til den nye.

Se scriptsene nedenfor, eller download filerne, inklusive bat-filer til at starte scriptsene. Download PowerShell-filerne og batchfilerne, der starter dem.

CleanUpDocLibs.ps1#

 1<#
 2Name: CleanUpDocLibs.ps1
 3Title: Clean Up Document Libraries
 4Author: Ulrich Gerting Bojko
 5
 6Description: Go through all Libraries (execpt the ones in the forbidden list)
 7and do the following:
 81. Allow Content Types
 92. Enable Versioning (major versions, no limit)
103. Attach Content Type "MyCustomDocument"
114. Delete Content Type "Document"
12#>
13#Fill ind the url of the sitecollection you wish to run through
14$url = "http://intranet.domain.com"
15
16#Fill in the Content Type you wish to add to the library
17$lookForCT = "MyCustomDocument"
18
19#Fill in the Content Type you wish to delete
20$lookForCTDel = "Document"
21
22#Fill out the names of those documentlibraries that should be excluded below
23$forbidden = @("Style Library", "FormServerTemplates", "SiteCollectionDocuments", "SiteAssets")
24
25#Do not edit below this line and blame me for anything gone wrong
26#------------------------------------------------------------------------------
27
28#Add the SharePoint snapin and continue
29Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue
30
31$site = Get-SPSite $url
32
33#Walk through each site in the site collection
34$site | Get-SPWeb -Limit all | ForEach-Object {
35$web = $_.Url
36<pre>`#Go through each document library in the site
37$_.Lists | where { $_.BaseTemplate -eq "DocumentLibrary"} | ForEach-Object { 
38
39    $docLibrary = $_.Lists[$_.Title]
40
41    #Check if List is forbidden
42    if ($forbidden -contains $_.Title ){
43
44    }
45    else
46    {
47
48        if (($_.ContentTypes | where { $_.Name -eq $lookForCT }) -eq $null)
49        {
50            write-host "Adding content type " $lookForCT "on list" $docLibrary.Title
51            $docLibrary.ContentTypesEnabled = $true
52                        $docLibrary.EnableVersioning = $true
53            $docLibrary.Update()
54
55            #Add site content types to the list
56            $ctToAdd = $site.RootWeb.ContentTypes[$lookForCT]
57            $ct = $docLibrary.ContentTypes.Add($ctToAdd)
58            write-host "Content type" $ct.Name "added to list" $docLibrary.Title
59            $docLibrary.Update()
60        }
61
62        #Delete Content Type
63        if (($_.ContentTypes | where { $_.Name -eq $lookForCTDel }) -eq $null)
64        {
65            #write-host "No content type exists with the name" $lookForCTDel "on list" $_.Title
66        }
67        else
68        {
69            $ctToRemove = $_.ContentTypes[$lookForCTDel]
70            write-host "Removing content type" $ctToRemove.Name "from list" $web "/" $_.Title
71            $_.ContentTypes.Delete($ctToRemove.Id)
72            $_.Update()
73        }
74    }
75}
76`</pre>
77}
78#Dispose of the site object
79$site.Dispose()

CleanUpItems.ps1#

 1<#
 2Name: CleanUpItems.ps1
 3Title: Clean Up Files in Document Libraries
 4Author: Ulrich Gerting Bojko
 5
 6Description: Go through all files in all Documentlibraries, that
 7have the new Content Type attached and do the following:
 81. Change the Content Type to the new one
 92. Preserve the "Modified" and "Editor" metadata
10
11#>
12#Fill ind the url of the sitecollection you wish to run through
13$url = "http://intranet.domain.com"
14
15#Fill in the name of the Content Tpe you wish to replace from
16$OldCTName = "Document"
17
18#Fill in the name of the Content Tpe you wish to replace to
19$NewCTName = "MyCustomDocument"
20
21#Do not edit below this line and blame me for anything gone wrong
22#------------------------------------------------------------------------------
23
24#Add the SharePoint snapin and continue
25Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue
26
27$site = Get-SPSite $url
28
29#Walk through each site in the site collection
30$site | Get-SPWeb -Limit all | ForEach-Object {
31<pre>`$web = $_.Url       
32
33#Go through each document library in the site
34$_.Lists | where { $_.BaseTemplate -eq "DocumentLibrary"} | ForEach-Object {
35$list = $_
36$oldCT = $list.ContentTypes[$OldCTName]
37$newCT = $list.ContentTypes[$NewCTName]
38$newCTID = $newCT.ID
39
40    #Check if the values specified for the content types actually exist on the list
41    if (($oldCT -ne $null) -and ($newCT -ne $null))
42    {
43        #Go through each item in the list
44        $list.Items | ForEach-Object {
45
46            #Check if the item content type currently equals the old content type specified
47            if ($_.ContentType.Name -eq $oldCT.Name)
48            {
49                #Check the check out status of the file
50                if ($_.File.CheckOutType -eq "None")
51                {
52                    #Change the content type association for the item
53                    $item = $_
54                    $Editor = $item["Editor"]
55                    $Modified = $item["Modified"]
56                    $_.File.CheckOut()
57                    write-host "Resetting content type for file" $web "/" $_.Url "from" $oldCT.Name "to" $newCT.Name
58                    $_["ContentTypeId"] = $newCTID
59                    $item["Editor"] = $Editor
60                    $item["Modified"] = $Modified
61                    $_.Update()
62                    $_.File.CheckIn("Content type changed to " + $newCT.Name, 1)
63                }
64                else
65                {
66                    write-host "File" $web "/" $_.Url "is checked out to" $_.File.CheckedOutByUser.ToString() "and cannot be modified"
67                }
68            }
69            else
70            {
71                write-host "File" $web "/" $_.Url "is associated with the content type" $_.ContentType.Name "and shall not be modified"
72
73            }
74        }
75    }
76    else
77    {
78        #write-host "One of the content types specified has not been attached to the list"$web $list.Title
79    }
80
81}
82`</pre>
83}
84#Dispose of the site object
85$site.Dispose()

Sidst opdateret

Del

Relaterede

Tags