magritton
2/26/2015 - 7:49 PM

This uses the client object model for SharePoint 2013 Online to upload a file to a specific folder

This uses the client object model for SharePoint 2013 Online to upload a file to a specific folder

public void sendDatatoSPO()
        {
            string strMetadata = "C:\\StoredMsgs\\Metadata.txt";

            string[] strLines = System.IO.File.ReadAllLines(strMetadata);

            ClientContext cix = new ClientContext("https://opwit.sharepoint.com/TestOCM");
            System.Security.SecureString passWord = new System.Security.SecureString();

            foreach (char c in "9ec4L8N9".ToCharArray()) passWord.AppendChar(c);
            cix.Credentials = new SharePointOnlineCredentials("mikegritton@opwit.onmicrosoft.com", passWord);
            cix.ExecuteQuery();

            List lx = cix.Web.Lists.GetByTitle("PF");
            foreach (var item in strLines)
            {
                string[] strMetaItems = item.Split('|');
                string strFilePath = "C:\\StoredMsgs\\Files\\" + strMetaItems[0] + "-" + strMetaItems[13];
                string strFolderName = strMetaItems[12].Replace("/sites/OPWSS/Teams/OCM", "");
                strFolderName = "/TestOCM" + strFolderName.Replace("/" + strMetaItems[13], "");
                //Folder fo = cix.Web.GetFolderByServerRelativeUrl("/TestOCM/pf/2014/Database");
                Folder fo = cix.Web.GetFolderByServerRelativeUrl(strFolderName);
                FileCreationInformation fci = new FileCreationInformation();
                fci.Url = strMetaItems[13];
                fci.Content = System.IO.File.ReadAllBytes(strFilePath); //new FileStream(strFilePath, FileMode.Open);
                fci.Overwrite = true;
                fo.Files.Add(fci);
                fo.Update();
                cix.ExecuteQuery();

                //string strRelUrl = strMetaItems[14];
                
                //WebClient wc = new WebClient();
                //wc.UploadFile(strUrl, strFilePath);
            }
        }