Creating Document Libraries
Creating a new document library using Windows PowerShell is very similar to the creation of any other type of list. We can use the same Add() method provided by the SPListCollection class. In the example below, we use the Get-SPWeb cmdlet to retrieve a specific site, store a TemplateType in a variable, and then use the Add() method to create a new document library:
PS > $spWeb = Get-SPWeb -Identity http://SPServer
PS > $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
PS > $spWeb.Lists.Add("My Documents","My Doc Library",$listTemplate)
PS > $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
PS > $spWeb.Lists.Add("My Documents","My Doc Library",$listTemplate)
Modifying a Document Library
We can retrieve an existing document library using the GetList() method, just as we did with lists. The only difference is the relative URL used with the method:
PS > $spDocumentLibrary = $spWeb.GetList("My Documents")
Next, we can modify the properties of a document library. If we want to change the Description, we can simply type:
PS > $spDocumentLibrary.Description = “Lots of Documents”
Adding Document Library to Quick Launch is also a simple task when using Windows PowerShell:
PS > $spDocumentLibrary.OnQuickLaunch = "True"
When we’re are done with the updates we use the Update() method to commit the changes.
PS > $spDocumentLibrary.Update()
SharePoint document libraries may have folders to better organize the contents of the library. These folders can be created using the same AddItem()method just as we did when adding new list items. The difference is that we use another overload definition of this method, which also accepts a value of type Microsoft.SharePoint.SPFileSystemObjectType that instructs it whether the new item is a file or a folder:
PS > $spFolder = $spDocumentLibrary.AddItem( >> "",[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,"My New Folder" >>) PS > $spFolder.Update()
Uploading Files to a Document Library
To upload files to a SharePoint document library, we use the Add method provided by the Microsoft.SharePoint.SPFileCollection class, which represents a collection of SPFile objects in SharePoint 2010.
Before we can access a file collection in SharePoint 2010, we have to create an instance of the Microsoft.SharePoint.SPFolder class using theGetFolder() method provided by the Microsoft.SharePoint.SPWeb class:
PS > $spFolder = $spWeb.GetFolder("My Documents")
After we have bound to the document library, we can store the file collection in a new variable, which we will use to add files:
PS > $spFileCollection = $spFolder.Files
The Add method provided by the Microsoft.SharePoint.SPFileCollection class is used to create a file in a file collection. This is a very versatile method that has 21 overload definitions. For the one that we will be using in our example, we need to specify the file’s relative URL, a byte array containing the file, and a Boolean value that determines whether an existing file with the same name that might already exist should be overwritten. Let’s have a look at the byte array first.
It is possible to expose a sequence of bytes using the System.IO.FileStream class, which we can pass on to the Add method. A simple way of retrieving an object of the type System.IO.FileStream is by using the OpenRead() method provided by the System.IO.FileInfo class. When using the Get-ChildItem cmdlet on a file, we get an object of the type System.IO.FileInfo:
PS > $file = Get-ChildItem C:\Documents\MyDoc.docx
Now we can use the OpenRead() method when adding a new file to a SharePoint library:
PS > $spFileCollection.Add("My Documents/MyDoc.docx",$file.OpenRead(),$false)
The example demonstrates how to upload a single file to a document library in SharePoint 2010. But what if we want to upload multiple files? Simply use the ForEach-Object cmdlet to loop through a collection of files and add them to a SharePoint 2010 document library using the Add() method:
PS > Get-ChildItem C:\Documents -filter “*.docx” | ForEach { >> $spFileCollection.Add(“My Documents/$($_.Name)”,$_.OpenRead(),$true) >>}
Comments
Post a Comment