SharePoint PowerShell Learning IV using PowerShell to manage document libraries

Source: Internet
Author: User

Use PowerShell to create a document library

The list of actions and actions for a document library is similar, and in this chapter, if you create a document library, upload a file to the document library

To create a document library, we also need to use the Add () method in SPListCollection.

PS > $spWeb = get-spweb-identity http://SPServer

PS > $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::D ocumentlibrary

PS > $spWeb. Lists.add ("My Documents", "My Doc Library", $listTemplate)

Update a document library with PowerShell

We can use the GetList () method to query the document library we need,

PS > $spDocumentLibrary = $spWeb. GetList ("My Documents")

Here we will modify some of the property information for this document library, such as the description of the document library and whether it is displayed in the Quick Launch.

PS > $spDocumentLibrary. Description = "Description"

PS > $spDocumentLibrary. onquicklaunch = "True"

PS > $spDocumentLibrary. Update ()

Folders may exist in a SharePoint document library, and folders can be used to manage files in a document library more efficiently. If we want to create a folder in a document library, we can use the AddItem method, just as we create a list item in the list. But the difference is that we need to overload another AddItem method, which means we need to add a Microsoft.SharePoint.SPFileSystemObjectType to the method to declare that we are adding a file or a folder.

PS > $spFolder = $spDocumentLibrary. AddItem (

>> "", [Microsoft.sharepoint.spfilesystemobjecttype]::folder, "My New folder"

>>)

PS > $spFolder. Update ()

Use PowerShell to upload a file to the document library

To upload files to a document library, we need to use the Add method in the Microsoft.SharePoint.SPFlieCollection class, which represents a collection of files.

Before we get the file collection, we also need to return the Microsoft.SharePoint.SPFolder through the GetFolder () method in the Microsoft.SharePoint.SPWeb class, which initializes an object variable.

PS > $spFolder = $spWeb. GetFolder ("My Documents")

At this point we can add files to this collection of files.

PS > $spFileCollection = $spFolder. Files

An Add method is provided in the Microsoft.SharePoint.SPFileCollection class to add a file to the collection of files, which is a very common method with 21 overloads defined. One of the things we'll be applying is that we need a file's absolute address, a file byte array, and a bool-type value that indicates whether to overwrite when there is a file with the same name.

We need to get to the file using the Get-childitem method class first. You can then get a byte array of the file by using the OpenRead () provided in the System.IO.FileStream class.

PS > $file = Get-childitem c:/documents/mydoc.docx

PS > $spFileCollection. ADD ("My Documents/mydoc.docx", $file. OpenRead (), $false)

Of course, we can also upload multiple documents to a SharePoint document library.

PS > Get-childitem c:/documents-filter "*.docx" | Foreach-object {

>> $spFileCollection. ADD ("My documents/$ $_. Name) ", $_. OpenRead (), $true)

>>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.