andyohn
andyohn

Reputation: 25

Create Folder in DocLib of a Teamsite - Office 365 - Sharepoint Online

I want to create an example folder in a Document Library of a created Teamsite on Sharepoint Online using Powershell but am running into an error.

After I have created the Teamsite I use the following script:

#Retrieve list
$DocLibName = "Dokumente"
$FolderTitle = "Beispiel"
$List = $ctx.Web.Lists.GetByTitle($DocLibName)
$folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
$folder["Title"] = $FolderTitle
$folder.Update(); 

$ctx.Load($List)
$ctx.ExecuteQuery()

Error Message

The type [Microsoft.SharePoint.SPFileSystemObjectType] was not found : Make sure that the assembly that contains this type is loaded.

 Line:79 Char:1
 + $ Folder = $ List.addItem ("" [Microsoft.SharePoint.SPFileSystemObjectType ] :: Folde ...

It is not possible to use an index to a null array.
Line:80 Char:1
+ $ Folder ["Title"] = $FolderTitle

It is not possible to call a method for an expression of the NULL .
Line:81 Char:1
+ $Folder.Update();

How can this be resolved?

Upvotes: 0

Views: 2069

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

You are getting this error since Microsoft.SharePoint.SPFileSystemObjectTypetype belongs to SharePoint Server Side API which is not compatible with Office 365.

Below is demonstrated how to create a folder in SharePoint Online site via PowerShell (utilizes SharePoint CSOM API)

Function Create-Folder()
{
Param(
  [Parameter(Mandatory=$True)]
  [Microsoft.SharePoint.Client.Folder]$ParentFolder, 

  [Parameter(Mandatory=$True)]
  [String]$FolderName
)

    $folder = $ParentFolder.Folders.Add($folderName)
    $ParentFolder.Context.Load($folder)
    $ParentFolder.Context.ExecuteQuery()
    return $folder
}

Function Get-Context($Url,$Username,$Password){
   $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
   $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
   $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
   $ctx.Credentials = $credentials
   return $ctx
}

Usage

$Url = "https://contoso.sharepoint.com/"
$UserName = "[email protected]"
$Password = ""
$TargetFolderName = "Archive2016"   


$ctx = Get-Context -Url $Url -Username $Username -Password $Password
$parentFolder = $ctx.Web.Lists.GetByTitle("Documents").RootFolder
$folder = Create-Folder -ParentFolder $parentFolder -FolderName $TargetFolderName
Write-Host "Folder [$TargetFolderName] has been created succesfully. Url: $($folder.ServerRelativeUrl)"

For creating a folder hierarchy the following script could be utilized:

Function Create-FolderHierarchy()
{
Param(
  [Parameter(Mandatory=$True)]
  [Microsoft.SharePoint.Client.Folder]$ParentFolder, 

  [Parameter(Mandatory=$True)]
  [String]$FolderUrl
)

    $folderNames = $FolderUrl.Trim().Split("/",[System.StringSplitOptions]::RemoveEmptyEntries)
    $folderName = $folderNames[0]
    $curFolder = $ParentFolder.Folders.Add($folderName)
    $ParentFolder.Context.Load($curFolder)
    $ParentFolder.Context.ExecuteQuery()
    if ($folderNames.Length -gt 1)
    {
        $curFolderUrl = [System.String]::Join("/", $folderNames, 1, $folderNames.Length - 1)
        return Create-FolderHierarchy -ParentFolder $curFolder -FolderUrl $curFolderUrl
    }
    return $curFolder 
}

If you are interested in scenario for uploading files while preserving a folder structure, take a look at How to: Upload files into Office 365 via PowerShell post, it contains ready made script for that purpose.

Upvotes: 1

Related Questions