Beispiel #1
0
        public void Add(TEntityType entity, ListAddOptions option = ListAddOptions.None)
        {
            var item = List.AddItem(new ListItemCreationInformation());

            mapper.MapEntityToItem(entity, item);
            item.Update();

            if ((option & ListAddOptions.DoNotExecuteQuery) != ListAddOptions.DoNotExecuteQuery)
            {
                List.Context.ExecuteQuery();
            }
        }
Beispiel #2
0
        public string Add(string fileName, byte[] fileBytes, TEntityType entity, LibraryAddOptions options = LibraryAddOptions.None)
        {
            var folder = List.RootFolder;

            List.Context.Load(folder, f => f.ServerRelativeUrl);
            List.Context.ExecuteQuery();

            var context = List.Context as ClientContext;

            if (context == null)
            {
                throw new NotImplementedException("Bin mir noch nicht sicher, wie ich das mache...");
            }

            var fileUrl = folder.ServerRelativeUrl + (folder.ServerRelativeUrl.EndsWith("/") || fileName.StartsWith("/") ? string.Empty : "/") + fileName;

            using (var stream = new System.IO.MemoryStream(fileBytes))
            {
                File.SaveBinaryDirect(context, fileUrl, stream, overwriteIfExists: true);
            }

            var file = List.ParentWeb.GetFileByServerRelativeUrl(fileUrl);

            List.Context.Load(file, f => f.ListItemAllFields, f => f.CheckOutType);
            List.Context.ExecuteQuery();

            var keepCheckedOut = (options & LibraryAddOptions.KeepCheckedOut) == LibraryAddOptions.KeepCheckedOut;

            if (entity != null)
            {
                mapper.MapEntityToItem(entity, file.ListItemAllFields);
                file.ListItemAllFields.Update();
                if (file.CheckOutType != CheckOutType.None && !keepCheckedOut)
                {
                    file.CheckIn("Checked in by application", CheckinType.OverwriteCheckIn);
                }
            }

            var doNotExecuteQuery = (options & LibraryAddOptions.DoNotExecuteQuery) == LibraryAddOptions.DoNotExecuteQuery;

            if (!doNotExecuteQuery)
            {
                List.Context.ExecuteQuery();
            }

            return(fileUrl);
        }