public void TestUpload()
        {
            Uri sourceUri = new Uri(@"D:\ivan\icon.pdf");
            Uri targetUri = new Uri(@"http://pensiob-sp2010/PensioB/Test/");

            byte[] contents = File.ReadAllBytes(sourceUri.LocalPath);

            ISharePointClient sharePointClient = GetSharePointService(targetUri);

            //Create Sharepoint document
            SharePointDocument targetSpDoc = new SharePointDocument(contents);

            string fileName = Path.GetFileName(sourceUri.LocalPath);
            fileName = string.Format(
                "{0}{1}{2}",
                targetUri.AbsolutePath,
                targetUri.OriginalString.EndsWith("/")
                    ? string.Empty
                    : "/",
                fileName);

            sharePointClient.UploadDocument(fileName, targetSpDoc);
        }
        public void TestUpload()
        {
            var sourceUri = new Uri(@"C:\Temp\temp.pdf");
            var targetUri = new Uri(@"http://Hoefnix/Test/a/");

            byte[] contents = File.ReadAllBytes(sourceUri.LocalPath);

            ISharePointClient sharePointClient = GetSharePointService(targetUri);

            //Create Sharepoint document
            var targetSpDoc = new SharePointDocument(contents);

            string fileName = Path.GetFileName(sourceUri.LocalPath);
            fileName = string.Format(
                "{0}{1}{2}",
                targetUri.AbsolutePath,
                targetUri.OriginalString.EndsWith("/")
                    ? string.Empty
                    : "/",
                fileName);

            string nr = sharePointClient.UploadDocumentReceiveVersion(fileName, targetSpDoc);
            Console.WriteLine(nr);
        }
        public void UploadDocument(string relativeUrl, SharePointDocument doc)
        {
            using (ClientContext spClientContext = GetSharePointClientContext())
            {
                //Check if the url exists
                int index = relativeUrl.LastIndexOf("/");
                string parentFolder = relativeUrl.Substring(0, index);

                //Create intermediate folders if not exist
                EnsureFolder(parentFolder);
                spClientContext.ExecuteQuery();

                try
                {
                    string targetUrl = string.Format("{0}{1}", SharePointSiteUrl, relativeUrl);

                    // Create a PUT Web request to upload the file.
                    WebRequest request = WebRequest.Create(targetUrl);

                    //Set credentials of the current security context
                    request.Credentials = CredentialCache.DefaultCredentials;
                    request.Method = "PUT";

                    // Create buffer to transfer file
                    byte[] fileBuffer = new byte[1024];

                    // Write the contents of the local file to the request stream.
                    using (Stream stream = request.GetRequestStream())
                    {
                        //Load the content from local file to stream
                        using (MemoryStream ms = new MemoryStream(doc.Content))
                        {
                            ms.Position = 0;

                            // Get the start point
                            int startBuffer = ms.Read(fileBuffer, 0, fileBuffer.Length);
                            for (int i = startBuffer; i > 0; i = ms.Read(fileBuffer, 0, fileBuffer.Length))
                            {
                                stream.Write(fileBuffer, 0, i);
                            }
                        }
                    }

                    // Perform the PUT request
                    WebResponse response = request.GetResponse();
                    if (response != null)
                    {
                        //Close response
                        response.Close();
                    }
                }
                catch (Exception e)
                {
                    s_Logger.Error(string.Format("UploadDocument({0}) failed using ClientContext {1}.", relativeUrl, SharePointSiteUrl), e);
                    throw;
                }
            }
        }
 public string UploadDocumentReceiveVersion(string relativeUrl, SharePointDocument doc)
 {
     throw new NotImplementedException();
 }
        /// <inheritdoc cref="ISharePointClient.UploadDocument" />
        public void UploadDocument(string relativeUrl, SharePointDocument doc)
        {
            if (doc == null || doc.Content == null)
            {
                return;
            }

            IDictionary<string, string> documentMap = GetDocumentMap();
            string fileName;
            if (documentMap.ContainsKey(relativeUrl))
            {
                fileName = documentMap[relativeUrl];
            }
            else
            {
                string extension = Path.GetExtension(relativeUrl);
                string tempFullFileName = Path
                    .GetTempFileName()
                    .Replace(".tmp", extension ?? ".bin");
                fileName = Path.GetFileName(tempFullFileName);
                documentMap.Add(relativeUrl, fileName);
                SaveDocumentMap(documentMap);
            }
            WriteFile(fileName, doc.Content);
        }
 public void UploadDocument(string relativeUrl, SharePointDocument doc)
 {
     Contract.Requires(!string.IsNullOrEmpty(SharePointSiteUrl));
     Contract.Requires(relativeUrl != null);
     Contract.Requires(!relativeUrl.StartsWith(SharePointSiteUrl));
     Contract.Requires(doc != null);
 }
        public void UploadDocument(string relativeUrl, SharePointDocument doc)
        {
            try
            {
                using (ClientContext spClientContext = GetSharePointClientContext())
                {
                    Web rootWeb = spClientContext.Site.RootWeb;

                    //Check if the url exists
                    int index = relativeUrl.LastIndexOf("/");
                    string parentFolder = relativeUrl.Substring(0, index);
                    string filename = relativeUrl.Substring(index + 1);

                    //Create intermediate folders if not exist
                    EnsureFolder(parentFolder);
                    Folder fldr = rootWeb.GetFolderByServerRelativeUrl(parentFolder);
                    spClientContext.ExecuteQuery();

                    //Create File information
                    var fciNewFileFromComputer = new FileCreationInformation
                    {
                        Content = doc.Content,
                        Url = filename,
                        Overwrite = true
                    };

                    //Upload the file
                    File uploadedFile = fldr.Files.Add(fciNewFileFromComputer);
                    spClientContext.Load(uploadedFile);
                    spClientContext.ExecuteQuery();
                }
            }
            catch (Exception e)
            {
                s_Logger.Error(string.Format("UploadDocument({0}) failed using ClientContext {1}.", relativeUrl, SharePointSiteUrl), e);
                throw;
            }
        }
        /// <inheritdoc cref="ISharePointClient.UploadDocument" />
        public void UploadDocument(string relativeUrl, SharePointDocument doc)
        {
            if (doc == null || doc.Content == null)
            {
                return;
            }

            string filePath = Path.Combine(s_SharepointMock, relativeUrl);
            using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
            {
                writer.Write(doc.Content);
            }
        }
 public string UploadDocumentReceiveVersion (string relativeUrl, SharePointDocument doc)
 {
     //NOP
     return default(string);
 }
 /// <inheritdoc cref="ISharePointClient.UploadDocument" />
 public void UploadDocument(string relativeUrl, SharePointDocument doc)
 {
     //NOP
 }
        private void Upload(string relativeUrl, SharePointDocument doc)
        {
            string targetUrl = string.Format("{0}{1}", SharePointSiteUrl, relativeUrl);

            // Create a PUT Web request to upload the file.
            WebRequest request = WebRequest.Create(targetUrl);

            //Set credentials of the current security context
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Method = "PUT";

            // Create buffer to transfer file
            byte[] fileBuffer = new byte[1024];

            // Write the contents of the local file to the request stream.
            using (Stream stream = request.GetRequestStream())
            {
                //Load the content from local file to stream
                using (MemoryStream ms = new MemoryStream(doc.Content))
                {
                    ms.Position = 0;

                    // Get the start point
                    int startBuffer = ms.Read(fileBuffer, 0, fileBuffer.Length);
                    for (int i = startBuffer; i > 0; i = ms.Read(fileBuffer, 0, fileBuffer.Length))
                    {
                        stream.Write(fileBuffer, 0, i);
                    }
                }
            }

            // Perform the PUT request
            WebResponse response = request.GetResponse();
            if (response != null)
            {
                //Close response
                response.Close();
            }
        }
        private string UploadDocumentRetrieveVersion(string relativeUrl, SharePointDocument doc, bool retrieveVersion)
        {
            using (ClientContext spClientContext = GetSharePointClientContext())
            {
                //Check if the url exists
                int index = relativeUrl.LastIndexOf("/");
                string parentFolder = relativeUrl.Substring(0, index);

                //Create intermediate folders if not exist
                EnsureFolder(parentFolder);
                spClientContext.ExecuteQuery();

                string result = null;

                try
                {
                    Upload(relativeUrl, doc);
                    if (retrieveVersion)
                    {
                        SharePointDocumentVersion sharePointDocumentVersion = RetrieveCurrentVersion(relativeUrl, spClientContext);
                        result = sharePointDocumentVersion.Version;
                    }
                }
                catch (Exception e)
                {
                    s_Logger.Error(string.Format("UploadDocument({0}) failed using ClientContext {1}.", relativeUrl, SharePointSiteUrl), e);
                    throw;
                }

                return result;
            }
        }
 public string UploadDocumentReceiveVersion(string relativeUrl, SharePointDocument doc)
 {
     return UploadDocumentRetrieveVersion(relativeUrl, doc, true);
 }
 public void UploadDocument(string relativeUrl, SharePointDocument doc)
 {
     UploadDocumentRetrieveVersion(relativeUrl, doc, false);
 }
 public string UploadDocumentReceiveVersion(string relativeUrl, SharePointDocument doc)
 {
     Contract.Requires(!string.IsNullOrEmpty(SharePointSiteUrl));
     Contract.Requires(relativeUrl != null);
     Contract.Requires(!relativeUrl.StartsWith(SharePointSiteUrl));
     Contract.Requires(doc != null);
     return default(string);
 }