Beispiel #1
0
        public static void UploadDemo()
        {
            const string siteUrl = "https://oauthplay.sharepoint.com";
            const string folderUrl = "/Shared Documents/DemoDocs";
            using (var ctx = new ClientContext(siteUrl))
            {
                var passWord = new SecureString();
                foreach (char c in "Pastries101".ToCharArray()) passWord.AppendChar(c);
                ctx.Credentials = new SharePointOnlineCredentials("*****@*****.**", passWord);

                var fileName = Path.GetTempFileName();
                System.IO.File.WriteAllText(fileName, "Test");
                var spFolder = ctx.Web.GetFolderByServerRelativeUrl(folderUrl);
                var file = UploadFileChunked(spFolder, fileName, "test.txt");
                var item = file.ListItemAllFields;

                const string template = "i:0#.f|membership|{0}@oauthplay.onmicrosoft.com";
                var modifiedBy = ctx.Web.EnsureUser(string.Format(template, "belindan"));
                var createdBy = ctx.Web.EnsureUser(string.Format(template, "allieb"));

                ctx.Load(modifiedBy);
                ctx.Load(createdBy);
                ctx.ExecuteQuery();

                item["Editor"] = modifiedBy.Id;
                var modifiedByField = new ListItemFormUpdateValue
                {
                    FieldName = "Modified_x0020_By",
                    FieldValue = modifiedBy.Id.ToString()
                };

                item["Author"] = createdBy.Id;
                var createdByField = new ListItemFormUpdateValue
                {
                    FieldName = "Created_x0020_By",
                    FieldValue = createdBy.Id.ToString()
                };

                item["Modified"] = DateTime.Now.Subtract(TimeSpan.FromDays(400)).ToUniversalTime();
                item["Created"] = DateTime.Now.Subtract(TimeSpan.FromDays(500)).ToUniversalTime();

                // it doesn't matter if you add both modifiedByField and createdByField.
                // As long as the list is non-empty all changes appear to carry over.
                var updatedValues = new List<ListItemFormUpdateValue> { modifiedByField, createdByField };
                item.ValidateUpdateListItem(updatedValues, true, string.Empty);
                ctx.Load(item);
                ctx.ExecuteQuery();

                System.IO.File.Delete(fileName);
                Console.WriteLine("Successfully uploaded document '{0}'", file.ServerRelativeUrl);
            }
        }
Beispiel #2
0
        public static void UploadDemo()

        {
            const string siteUrl   = "https://oauthplay.sharepoint.com";
            const string folderUrl = "/Shared Documents/DemoDocs";

            using (var ctx = new ClientContext(siteUrl))
            {
                var passWord = new SecureString();
                foreach (char c in "Pastries101".ToCharArray())
                {
                    passWord.AppendChar(c);
                }
                ctx.Credentials = new SharePointOnlineCredentials("*****@*****.**", passWord);


                var fileName = Path.GetTempFileName();
                System.IO.File.WriteAllText(fileName, "Test");
                var spFolder = ctx.Web.GetFolderByServerRelativeUrl(folderUrl);
                var file     = UploadFileChunked(spFolder, fileName, "test.txt");
                var item     = file.ListItemAllFields;


                const string template   = "i:0#.f|membership|{0}@oauthplay.onmicrosoft.com";
                var          modifiedBy = ctx.Web.EnsureUser(string.Format(template, "belindan"));
                var          createdBy  = ctx.Web.EnsureUser(string.Format(template, "allieb"));

                ctx.Load(modifiedBy);
                ctx.Load(createdBy);
                ctx.ExecuteQuery();

                item["Editor"] = modifiedBy.Id;
                var modifiedByField = new ListItemFormUpdateValue
                {
                    FieldName  = "Modified_x0020_By",
                    FieldValue = modifiedBy.Id.ToString()
                };

                item["Author"] = createdBy.Id;
                var createdByField = new ListItemFormUpdateValue
                {
                    FieldName  = "Created_x0020_By",
                    FieldValue = createdBy.Id.ToString()
                };

                item["Modified"] = DateTime.Now.Subtract(TimeSpan.FromDays(400)).ToUniversalTime();
                item["Created"]  = DateTime.Now.Subtract(TimeSpan.FromDays(500)).ToUniversalTime();

                // it doesn't matter if you add both modifiedByField and createdByField.
                // As long as the list is non-empty all changes appear to carry over.
                var updatedValues = new List <ListItemFormUpdateValue> {
                    modifiedByField, createdByField
                };
                item.ValidateUpdateListItem(updatedValues, true, string.Empty);
                ctx.Load(item);
                ctx.ExecuteQuery();


                System.IO.File.Delete(fileName);
                Console.WriteLine("Successfully uploaded document '{0}'", file.ServerRelativeUrl);
            }
        }