Wednesday, April 25, 2012

How To Upload file Into a SharePoint Document Library
[Upload file programmatically into given folder path ]
(Data Migration P2)


This is directly bind with the previous post (How To Create Folder Structure in SharePoint Document Library) . There I explained that I got a task on Data Migration move file from master file share into SharePoint Site. there i had to consider on two different sub task mainly

  1. How to create folder structure which same as physical path of the file (which I explained in Data Migration P1)
  2. How to upload the required document form file base to that newly created location in the document library (which I'm going to explain in Data Migration P2)

The Requirement ....

Original file : C:\swsetup\SP51083\Vista\example.docx

SharePoint Location :http://talliance.domainx.local:12345/DocLibName/swsetup/SP51083/Vista/


highlighted area represent the directory path which they need to upload the file (I have discussed how to create this inside the document library)
highlighted area represent the document library which they need to upload the file.


How to do that ?? 


For this I have introduce a function which responsible to upload the required file into the correct location.


spServerURL : The URL of the application [Eg: http://myspweb:8000/]

spDocumenttargetUrl :  the name of the document library which you need to upload documents 

folder : location of the doc lib which need to upload our file

FileToUpload : the physical location of the file which need to upload (Eg : C:\swsetup\SP51083\Vista\example.docx)


Overwirte : saying where file should be overwrite, if it existing or leave it as it is.  



public static void CreateSPFile(string spServerURL, string spDocumenttargetUrl, string folder, string fileToUpload, bool overwrite)
        {
            // I suggest skip this pre check since it internally opens a new site object
            // If you have to silenlty ignore non-existant SPSite you should catch a FileNotFoundException.
            if (SPSite.Exists(new Uri(spServerURL)))
            {
                // use the using construct to safely dispose the opened SPSite object
                using (SPSite site = new SPSite(spServerURL))
                {
                    // SPWeb object opened with SPSite.OpenWeb() have to be disposed as well
                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;

                        string targetUrl = string.Concat("\\", spDocumenttargetUrl);

                        if (!String.IsNullOrEmpty(folder))
                        {
                            targetUrl = string.Concat(targetUrl + folder);
                        }

                        SPFolder target = web.GetFolder(targetUrl);
                        String fileName = System.IO.Path.GetFileName(fileToUpload);
                        FileStream fileStream = File.OpenRead(fileToUpload);
                        SPFileCollection files = target.Files;
                        target.Files.Add(fileName, fileStream, overwrite);

                    }
                }
            }
        }

Let's Move from Line by Line ....
I'm gonna explain, only the most important lines in this function.

SPWeb web = site.OpenWeb()
web.AllowUnsafeUpdates = true;


when you have a list and if you want to update something, then you need to set AllowUnsafeUpdates = true for the web and the best practice is, after you have done you need to set it back to false. but in here i haven't make it false coz this process going through a massive loop so it'take time and might be crashed. and also "Setting this property to true opens security risks, potentially introducing cross-site scripting vulnerabilities" (by msdn).

string targetUrl = string.Concat("\\", spDocumenttargetUrl);

                        if (!String.IsNullOrEmpty(folder))
                        {
                            targetUrl = string.Concat(targetUrl + folder);
                        }
Crete target folder URL by combining Folder location and document library relative URL
    

String fileName = System.IO.Path.GetFileName(fileToUpload);
                        FileStream fileStream = File.OpenRead(fileToUpload);

Convert the physical file into the data stream which we are going to add into the required location.

SPFileCollection files = target.Files;
Get the file collection on the target location.

target.Files.Add(fileName, fileStream, overwrite);
Add the stream into the required location.




 ok That's It ... 
HAPPY CODING

No comments:

Post a Comment