C# – ssis script task to create folders in Sharepoint document library

csharepointssis

I am trying to create an SSIS package that gets a folder tree structure from a remote SQL DB and recreates that structure in a document library in Sharepoint.

I tried to (hard) code a Script Task to create just one folder but I'm getting this error:
http://img856.imageshack.us/img856/1386/helpel.png

After running only a part of this script:

Microsoft.Sharepoint.Client.dll
Microsoft.Sharepoint.Client.Runtime.dll

public void Main()
    {
        ClientContext clientContext = new ClientContext("http://spdemo.example.com/");
        clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "SPDEMO");
        Web rootWeb = clientContext.Web;

        Dts.TaskResult = (int)ScriptResults.Success;
    }

I've scoured the internet for solutions, but haven't found something that works for me.

So basically i need to find out how to:

  1. create a folder
  2. populate it with sub-folders
  3. Copy files in bitestreams from SQL to Sharepoint, all in SSIS

Thanks in advance!
Regards,
Vlad Ardelean

Best Answer

After some research I found out that in order to reference a DLL in a SSIS Script Task it first has to be strong named

Instead, I have found an easier way to create folders and upload files:

public void CreateFolder(string url)
    {
        HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(url);
        request.Credentials = new NetworkCredential(user, pass);
        request.Method = "MKCOL";
        HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
        response.Close();
    }

public void UploadDocument(byte[] file, string destinationName)
    {
        byte[] buffer = file;

        WebRequest request = WebRequest.Create(destinationName);
        request.Credentials = new System.Net.NetworkCredential(user, pass);
        request.Method = "PUT";
        request.ContentLength = buffer.Length;

        BinaryWriter writer = new BinaryWriter(request.GetRequestStream());
        writer.Write(buffer, 0, buffer.Length);
        writer.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        response.Close();
    }

url : represents the url path of the folder you want to create

http://spsite.host.com/DocLib/FolderToCreate

destinationName: path + document name

http://spsite.host.com/DocLib/FolderToCreate/DocToCreate.docx
Related Topic