Skip to main content

PowerShell Commands for Managing Libraries



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)
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

Popular posts from this blog

Site Logo Not Changing on Web Part Pages

I tested and reproduced your issue in my local machine. Since the Web Part Pages would override the content in PlaceHolderPageTitleInTitleArea place holder, the site logo would not change automatically. So would you please try remove or comment the following control TitleBarWebPart: See the similar scenario and solution: http://emanonsolutions.blogspot.com/2010/02/left-navigation-webpart-pages.html Hope this can help.

Create a Custom Site Definition with Additional Content in SharePoint 2010 Using Visual Studio 2010

·          Web Templates ·          Site Definitions and Configurations ·          Deciding Between Custom Web Templates and Custom Site Definitions ·          Understanding Onet.xml Files ·          How to: Create a Custom Web Template ·          Overview of Creating Custom Site Definitions Site Template Configurator utility http://stefan-stanev-sharepoint-blog.blogspot.com/search/label/SharePoint%202010 Create a Custom Site Definition with Additional Content in SharePoint 2010 Using Visual Studio 2010 http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2010/11/11/sharepoint-2010-cookbook-how-to-create-a-custom-site-definition-with-additional-content-in-sharepoint-2010-using-visual-studio-2010.aspx http://blogs.msdn.com/b/allenwang/...

SPFx Fantastic 40 Web Parts

SPFx Fantastic 40 Web Parts Ref Link :  https://github.com/OlivierCC/spfx-40-fantastics Menu & Carousels & News Management Overview Web Part Description News Carousel Insert a classical, responsive, cool & touch ready News Carousel. With this web part, you can add easily news focus in your SharePoint site. The users can easily navigate in news items, with buttons or with touch. Tiles Menu This Web Part allows you to very easily create a menu in form of tiles that is responsive and adapted for mobile. You can directly manage the items on your menu, with a title, an image and manage Visual rendering options. 3D Carousel Insert a 3D Carousel in your SharePoint pages. With this Web Part, you can manage your menu items and create automatically a 3D carousel. Coverflow Generates a Coverflow Apple like menu in your pages. Manage your menu items with title and picture and create a cool coverflow menu. News Slider Insert a News Slider Tiles control to your pages....