Programmatically copy the entire folder and check in to SharePoint

Source: Internet
Author: User
Tags pointweb
Document directory
  • 30 minutes later...
  • Recursive directory Replication
  • Recursively checkout Sharepoint files by programming
  • Recursively checkin Sharepoint files through programming

Since the author of this article deployed SharePoint 2010 for his company, the IT department where IT is located has begun to flood into many questions about how to use SharePoint 2010. People in the company understand the principles of SharePoint, so it is easy to explain the problem. One of the most prominent problems was that they used file servers to store files. Now they want to migrate those contents to SharePoint so that they can use the search function. So they started copying files to our SharePoint instance. But then they suddenly realized that doing so was a huge task, especially when faced with a file repository containing five years of content growth. If you perform this operation on the SharePoint user interface, the process will be slow. So they asked if they could copy and paste files as quickly as they would in the file system? My answer is yes, because the SharePoint document library has a built-in function that allows you to open the Windows Resource Manager view. You can see the location and description of this function:

[Windows Resource Manager view in Sharepoint]

Click this icon to open the Windows resource manager. You can drag and drop files as normal in the file system. But then they realized another problem. The uploaded files could not be found in SharePoint. I told them to try again 30 minutes later. The newly added content will not be displayed in the search in real time, because the content needs to be indexed by SharePoint first.

30 minutes later...

They still cannot search. I once read that all the documents are indeed placed in the correct position, but wait a moment, the icons of these documents have a green down arrow, this means that the version control function is enabled for this document library. By default, documents are checked out and never checked in. They are not displayed in the search results unless they are checked in at least once throughout the lifecycle of the document. So I told them that they needed to manually select those documents, click the check-in button, and then click OK in the pop-up check-in dialog box. They are very satisfied.
Note:If the version control of the document library is not enabled, the check-in and check-out will not occur.

Recently, when I read Stack Overflow Information on the Internet, I saw a reply to the Forum about copying files to SharePoint through programming, I suddenly realized why I didn't think of using this method at the time. This is also the goal of this article.

REFERENCE The following naming controls. You 'd better have the SharePoint 2010 SDK at hand.

using System;using Microsoft.SharePoint;using System.IO;

First, you need a recursive function to copy files and directories from one location to another, and because files in SharePoint can be located through the file System, it is easy to use System. IO class for processing. There is a simple way to know which directory you want to copy. You only need to check the address of the Windows Resource Manager view mentioned above. Generally, if it is in the form of http://test.com/subfolder, then the target location we copied should be mongotest.comw.subfolder. simply remove http and use a backslash instead of a slash.

Recursive directory Replication
public static void RecursiveCopy(string sSourceFolder, string sDestinationFolder){    if (!Directory.Exists(sDestinationFolder))    {        Directory.CreateDirectory(sDestinationFolder);    }    string[] aFiles = Directory.GetFiles(sSourceFolder);    foreach (string sFile in aFiles)    {        string sFileName = Path.GetFileName(sFile);        string sDestination = Path.Combine(sDestinationFolder, sFileName);        File.Copy(sFile, sDestination);    }    string[] aFolders = Directory.GetDirectories(sSourceFolder);    foreach (string sFolder in aFolders)    {        string sFileNameSub = Path.GetFileName(sFolder);        string sDestinationSub = Path.Combine(sDestinationFolder, sFileNameSub);        RecursiveCopy(sFolder, sDestinationSub);    }}

This Code uses a function to recursively check files. In your environment, you only need to replace the address in the SPSite constructor parameter with the http address of your actual Team website, and change the document library name after SPDocumentLibrary to your document library name.

Recursively checkout Sharepoint files by programming
Public static void RecursiveMassCheckOut () {using (SPSite oSharepointSite = new SPSite ("http://sharepoint.com/MyTeamSite") {using (SPWeb oSharepointWeb = oSharepointSite. openWeb () {SPDocumentLibrary ow.pointdocs = (SPDocumentLibrary) ow.pointweb. lists ["MyDocumentLibrary"]; int iFolderCount = ow.pointdocs. folders. count; // check out the file MassCheckOut (ow.pointdocs. rootFolder); // check out the file for (int I = 0; I <iFolderCount; I ++) {MassCheckOut (o1_pointdocs. folders [I]. folder) ;}}} public static void MassCheckOut (SPFolder oSharepointFolder) {foreach (SPFile oSharepointFiles in oSharepointFolder. files) {if (ow.pointfiles. checkOutType = SPFile. SPCheckOutType. none) {ow.pointfiles. checkOut ();}}}

As a useful supplement, the code for recursively checking out a file is listed here.

Recursively checkin Sharepoint files through programming

Return to the topic. The following code completes the check-in.

Public static void RecursiveMassCheckIn () {using (SPSite oSharepointSite = new SPSite ("http://sharepoint.com/MyTeamSite") {using (SPWeb oSharepointWeb = oSharepointSite. openWeb () {SPDocumentLibrary ow.pointdocs = (SPDocumentLibrary) ow.pointweb. lists ["MyDocumentLibrary"]; int iFolderCount = ow.pointdocs. folders. count; // check the file MassCheckIn (ow.pointdocs. rootFolder); // check the file in the subdirectory (Int I = 0; I <iFolderCount; I ++) {MassCheckIn (o1_pointdocs. folders [I]. folder) ;}}} public static void MassCheckIn (SPFolder oSharepointFolder) {foreach (SPFile oSharepointFiles in oSharepointFolder. files) {if (ow.pointfiles. checkOutType! = SPFile. SPCheckOutType. None) {oSharepointFiles. CheckIn ("this file is checked in programmatically ");}}}

At the beginning, I used. NET Framework 4 and encountered the following error during compilation:

 Unhandled Exception:System.PlatformNotSupportedException:Microsoft SharePoint is not supported with version 4.0.30319.1 of the Microsoft .Net Runtime.

at Microsoft.SharePoint .Administration.SPConfigurationDatabase.get_Farm()

at Microsoft.SharePoint.Administration.SPFarm.FindLocal(SPFarm& farm. Boolean & isJoined)

at Microsoft.SharePoint.SPSite..ctor(String requestUrl)

at SharepointCopy.MassCheckOut()

at SharePointCopy.Process()

at Program.Main(String[] args)

It seems that. NET 4 is not supported, so it is best to compile in. NET Framework3.5. After the. NET Framework version is modified, compilation is normal.

Now, you can call a command line like this:

 RecursiveCopy(@"C:\LocalFolder\", @"\\sharepoint.com\MyTeamSite\MyDocumentLibrary\");
RecursiveMassCheckIn();

 

References

Programatically Copy and Check In a Full Directory to Sharepoint

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.