Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 28 additions & 32 deletions Content Types/Create/Create a content type/CreateContentType.ps1
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
function New-SPOContentType
{
param(
[Parameter(Mandatory=$true,Position=1)]
function New-SPOContentType{
param(
[Parameter(Mandatory=$true,Position=1)]
[string]$Username,
[Parameter(Mandatory=$true,Position=2)]
$AdminPassword,
[Parameter(Mandatory=$true,Position=3)]
[Parameter(Mandatory=$true,Position=3)]
[string]$Url
)

$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$ctx.ExecuteQuery()


$lci =New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
$lci.Description="Description"
$lci.Name="Powershell Content Type222Task Based"
#$lci.ID="0x0108009e862727eed04408b2599b25356e7914"
$lci.ParentContentType=$ctx.Web.ContentTypes.GetById("0x01")
$lci.Group="List Content Types"

$ContentType = $ctx.Web.ContentTypes.Add($lci)
$ctx.Load($contentType)
try
{

$ctx.ExecuteQuery()
Write-Host "Content Type " $Title " has been added to " $Url
}
catch [Net.WebException]
{
Write-Host $_.Exception.ToString()
}

)

$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$ctx.ExecuteQuery()


$lci =New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
$lci.Description="Description"
$lci.Name="Powershell Content Type222Task Based"
#$lci.ID="0x0108009e862727eed04408b2599b25356e7914"
$lci.ParentContentType=$ctx.Web.ContentTypes.GetById("0x01")
$lci.Group="List Content Types"

$ContentType = $ctx.Web.ContentTypes.Add($lci)
$ctx.Load($contentType)

try{
$ctx.ExecuteQuery()
Write-Host "Content Type " $Title " has been added to " $Url
}
catch [Net.WebException]{
Write-Host $_.Exception.ToString()
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
A short script to create a content type and add it to a content type hub.

The script is fully described in an article here:

http://social.technet.microsoft.com/wiki/contents/articles/31051.sharepoint-online-content-types-in-powershell.aspx










The script is fully described in an article [here](http://social.technet.microsoft.com/wiki/contents/articles/31051.sharepoint-online-content-types-in-powershell.aspx
).

### How to use?

1. Download and install [SharePoint Online SDK](https://www.microsoft.com/en-us/download/details.aspx?id=42038).


1. Download and install SharePoint Online SDK.

2. Download the .ps1 file.
2. Download the *.ps1* file.

3. Open the file (you can do it also in NotePad)

4. Insert your data in these lines:





```PowerShell
# Paths to SDK. Please verify location on your computer.
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Expand All @@ -44,25 +27,15 @@ $Name="Name of the Content Type2"
$ParentContentTypeID="0x01"
$Group="List Content Types"
```
a) Find on your computer where SharePoint.Clitent.dll and SharePoint.Client.Runtime.dll libraries are located and insert the correct paths
b) Instead of "admin@tenant.onmicrosoft.com" enter you username
c) Instead of TENANT in "https://tenant.sharepoint.com/sites/contenttypehub" enter the name of your tenant
d) Fill in the properties of the content type.



a) Find on your computer where SharePoint.Clitent.dll and SharePoint.Client.Runtime.dll libraries are located and insert the correct paths </br>
b) Instead of "admin@tenant.onmicrosoft.com" enter you username </br>
c) Instead of TENANT in "https://tenant.sharepoint.com/sites/contenttypehub" enter the name of your tenant </br>
d) Fill in the properties of the content type. </br>

5. Run the script in Powershell (any module).

6. When the script has executed, Powershell will show a message Content Type Name of the Content Type2 has been added to Url of the Site.
6. When the script has executed, Powershell will show a message *Content Type Name of the Content Type2 has been added to Url of the Site*.









<br/><br/>
<b>Enjoy and please share feedback!</b>
Original file line number Diff line number Diff line change
@@ -1,58 +1,50 @@
function Get-SPOContentType
{
param (
[Parameter(Mandatory=$true,Position=1)]
function Get-SPOContentType{
param (
[Parameter(Mandatory=$true,Position=1)]
[string]$Username,
[Parameter(Mandatory=$true,Position=2)]
$AdminPassword,
[Parameter(Mandatory=$true,Position=3)]
[Parameter(Mandatory=$true,Position=3)]
[string]$Url
)

$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$ctx.ExecuteQuery()

$ctx.Load($ctx.Web.Lists)
$ctx.Load($ctx.Web.Webs)
$ctx.ExecuteQuery()
Write-Host
Write-Host $ctx.Url -BackgroundColor White -ForegroundColor DarkGreen
foreach( $ll in $ctx.Web.Lists)
{

$ctx.Load($ll.ContentTypes)
)

$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$ctx.ExecuteQuery()

try{
$ctx.ExecuteQuery()
}
catch{
#do nothing
}
$ctx.Load($ctx.Web.Lists)
$ctx.Load($ctx.Web.Webs)
$ctx.ExecuteQuery()
Write-Host
Write-Host $ctx.Url -BackgroundColor White -ForegroundColor DarkGreen

foreach( $ll in $ctx.Web.Lists){
$ctx.Load($ll.ContentTypes)

foreach($cc in $ll.ContentTypes)
{
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Title($cc.Name)
$obj | Add-Member NoteProperty List($ll.Title)
$obj | Add-Member NoteProperty Web($url)
try{
$ctx.ExecuteQuery()
}
catch{
#do nothing
}

Write-Output $obj
}

foreach($cc in $ll.ContentTypes){
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Title($cc.Name)
$obj | Add-Member NoteProperty List($ll.Title)
$obj | Add-Member NoteProperty Web($url)

}
Write-Output $obj
}
}


if($ctx.Web.Webs.Count -gt 0)
{
foreach ($web in $ctx.Web.Webs)
{
Get-SPOContentType -Username $Username -Url $web.Url -AdminPassword $AdminPassword
}
}

if($ctx.Web.Webs.Count -gt 0){
foreach ($web in $ctx.Web.Webs){
Get-SPOContentType -Username $Username -Url $web.Url -AdminPassword $AdminPassword
}
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,43 @@
function Get-SPOContentType
{

param (
[Parameter(Mandatory=$true,Position=1)]
function Get-SPOContentType{
param (
[Parameter(Mandatory=$true,Position=1)]
[string]$Username,
[Parameter(Mandatory=$true,Position=2)]
$AdminPassword,
[Parameter(Mandatory=$true,Position=3)]
[Parameter(Mandatory=$true,Position=3)]
[string]$Url,
[Parameter(Mandatory=$true,Position=4)]
[Parameter(Mandatory=$true,Position=4)]
[string]$NameOrTitle
)

$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$ctx.ExecuteQuery()

$ctx.Load($ctx.Web)
$ctx.Load($ctx.Web.ContentTypes)
$ctx.ExecuteQuery()
)

$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$ctx.ExecuteQuery()

foreach($cc in $ctx.Web.ContentTypes)
{

$column=$cc.Fields.GetByInternalNameOrTitle($NameOrTitle.Trim())
$ctx.Load($column)
$ErrorActionPreference= 'silentlycontinue'
try
{
$ctx.ExecuteQuery()
}
catch [Net.WebException]
{
}

if($column.Title -eq $NameOrTitle.Trim())
{
Write-Host $cc.Name
}
}





}

$ctx.Load($ctx.Web)
$ctx.Load($ctx.Web.ContentTypes)
$ctx.ExecuteQuery()


foreach($cc in $ctx.Web.ContentTypes){
$column=$cc.Fields.GetByInternalNameOrTitle($NameOrTitle.Trim())
$ctx.Load($column)
$ErrorActionPreference= 'silentlycontinue'

try{
ctx.ExecuteQuery()
}
catch [Net.WebException]{}




if($column.Title -eq $NameOrTitle.Trim()){
Write-Host $cc.Name
}
}
}


# Paths to SDK. Please verify location on your computer.
# Paths to SDK. Please verify location on your computer.
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Expand Down
Loading