public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action<object> action) { var folderModelHost = modelHost.WithAssertAndCast<FolderModelHost>("modelHost", value => value.RequireNotNull()); var folderModel = model.WithAssertAndCast<FolderDefinition>("model", value => value.RequireNotNull()); if (folderModelHost.CurrentList != null && folderModelHost.CurrentList.BaseType == BaseType.DocumentLibrary) { var currentFolder = EnsureLibraryFolder(folderModelHost, folderModel); var newContext = new FolderModelHost { CurrentList = folderModelHost.CurrentList, CurrentLibraryFolder = currentFolder, CurrentWeb = folderModelHost.CurrentWeb //Folder = currentFolder, //List = folderModelHost.CurrentList, //Web = folderModelHost.CurrentWeb }; action(newContext); } else if (folderModelHost.CurrentList != null && folderModelHost.CurrentList.BaseType != BaseType.DocumentLibrary) { var currentListItem = EnsureListFolder(folderModelHost, folderModel); var newContext = new FolderModelHost { CurrentList = folderModelHost.CurrentList, CurrentListItem = currentListItem, CurrentWeb = folderModelHost.CurrentWeb }; action(newContext); } else { throw new NotImplementedException("model host is unklnown"); } }
protected Folder GetLibraryFolder(FolderModelHost folderModelHost, FolderDefinition folderModel) { var parentFolder = folderModelHost.CurrentLibraryFolder; var context = parentFolder.Context; context.Load(parentFolder, f => f.Folders); context.ExecuteQuery(); // dirty stuff, needs to be rewritten var currentFolder = parentFolder .Folders .OfType<Folder>() .FirstOrDefault(f => f.Name == folderModel.Name); if (currentFolder != null) { context.Load(currentFolder, f => f.Name); context.ExecuteQuery(); } return currentFolder; }
protected Folder GetListFolder(FolderModelHost folderModelHost, FolderDefinition folderModel) { var tmp = string.Empty; var result = GetListFolder(folderModelHost, folderModel, out tmp); return result; }
private void ProcessWebFolder(FolderModelHost folderHost, ModuleFileDefinition moduleFile) { var web = folderHost.HostWeb; var folder = folderHost.CurrentWebFolder; var context = web.Context; if (!folder.IsPropertyAvailable("ServerRelativeUrl")) { context.Load(folder, f => f.ServerRelativeUrl); context.ExecuteQueryWithTrace(); } var currentFile = web.GetFileByServerRelativeUrl(GetSafeFileUrl(folder, moduleFile)); context.Load(currentFile, f => f.Exists); context.ExecuteQueryWithTrace(); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentFile.Exists ? currentFile : null, ObjectType = typeof(File), ObjectDefinition = moduleFile, ModelHost = folderHost }); if (moduleFile.Overwrite) { var fileCreatingInfo = new FileCreationInformation { Url = moduleFile.FileName, Overwrite = true }; if (moduleFile.Content.Length < ContentStreamFileSize) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Using fileCreatingInfo.Content for small file less than: [{0}]", ContentStreamFileSize); fileCreatingInfo.Content = moduleFile.Content; } else { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Using fileCreatingInfo.ContentStream for big file more than: [{0}]", ContentStreamFileSize); fileCreatingInfo.ContentStream = new System.IO.MemoryStream(moduleFile.Content); } var file = folder.Files.Add(fileCreatingInfo); folder.Context.ExecuteQueryWithTrace(); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = file, ObjectType = typeof(File), ObjectDefinition = moduleFile, ModelHost = folderHost }); } else { InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentFile.Exists ? currentFile : null, ObjectType = typeof(File), ObjectDefinition = moduleFile, ModelHost = folderHost }); } folder.Update(); folder.Context.ExecuteQueryWithTrace(); }
private File ProcessFile(FolderModelHost folderHost, ModuleFileDefinition moduleFile) { var context = folderHost.CurrentListFolder.Context; var web = folderHost.CurrentWeb; var list = folderHost.CurrentList; var folder = folderHost.CurrentListFolder; context.Load(folder, f => f.ServerRelativeUrl); context.Load(folder, f => f.Properties); context.ExecuteQueryWithTrace(); var stringCustomContentType = ResolveContentTypeId(folderHost, moduleFile); if (list != null) { context.Load(list, l => l.EnableMinorVersions); context.Load(list, l => l.EnableVersioning); context.Load(list, l => l.EnableModeration); context.ExecuteQueryWithTrace(); } var file = web.GetFileByServerRelativeUrl(GetSafeFileUrl(folder, moduleFile)); context.Load(file, f => f.Exists); context.ExecuteQueryWithTrace(); InvokeOnModelEvent<ModuleFileDefinition, File>(file, ModelEventType.OnUpdating); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = file.Exists ? file : null, ObjectType = typeof(File), ObjectDefinition = moduleFile, ModelHost = folderHost }); var doesFileHasListItem = //Forms folders !(folder != null && (folder.Properties.FieldValues.ContainsKey("vti_winfileattribs") && folder.Properties.FieldValues["vti_winfileattribs"].ToString() == "00000012")); WithSafeFileOperation(list, file, f => { var fileName = moduleFile.FileName; var fileContent = moduleFile.Content; var fileCreatingInfo = new FileCreationInformation { Url = fileName, Overwrite = file.Exists }; if (fileContent.Length < ContentStreamFileSize) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Using fileCreatingInfo.Content for small file less than: [{0}]", ContentStreamFileSize); fileCreatingInfo.Content = fileContent; } else { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Using fileCreatingInfo.ContentStream for big file more than: [{0}]", ContentStreamFileSize); fileCreatingInfo.ContentStream = new System.IO.MemoryStream(fileContent); } TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Overwriting file"); var updatedFile = folder.Files.Add(fileCreatingInfo); if (!string.IsNullOrEmpty(stringCustomContentType)) updatedFile.ListItemAllFields[BuiltInInternalFieldNames.ContentTypeId] = stringCustomContentType; if (moduleFile.DefaultValues.Count > 0) EnsureDefaultValues(updatedFile.ListItemAllFields, moduleFile); if (!string.IsNullOrEmpty(stringCustomContentType) || moduleFile.DefaultValues.Count > 0) updatedFile.ListItemAllFields.Update(); return updatedFile; }, doesFileHasListItem); var resultFile = web.GetFileByServerRelativeUrl(GetSafeFileUrl(folder, moduleFile)); context.Load(resultFile, f => f.Exists); context.ExecuteQueryWithTrace(); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = resultFile, ObjectType = typeof(File), ObjectDefinition = moduleFile, ModelHost = folderHost }); InvokeOnModelEvent<ModuleFileDefinition, File>(resultFile, ModelEventType.OnUpdated); return resultFile; }
protected string ResolveContentTypeId(FolderModelHost folderHost, ModuleFileDefinition moduleFile) { var context = folderHost.CurrentListFolder.Context; var list = folderHost.CurrentList; var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(moduleFile.ContentTypeId)) { stringCustomContentType = moduleFile.ContentTypeId; } else if (!string.IsNullOrEmpty(moduleFile.ContentTypeName)) { // preload custom content type var listContentTypes = list.ContentTypes; context.Load(listContentTypes); context.ExecuteQueryWithTrace(); var listContentType = listContentTypes.ToList() .FirstOrDefault(c => c.Name.ToUpper() == moduleFile.ContentTypeName.ToUpper()); if (listContentType == null) { throw new ArgumentNullException( string.Format("Cannot find content type with Name:[{0}] in List:[{1}]", new string[] { moduleFile.ContentTypeName, list.Title })); } stringCustomContentType = listContentType.Id.ToString(); } return stringCustomContentType; }
private Folder GetListFolder(FolderModelHost folderModelHost, FolderDefinition folderModel, out string serverRelativeUrl) { var list = folderModelHost.CurrentList; var context = list.Context; context.Load(list, l => l.RootFolder); context.Load(list, l => l.ParentWeb); #if NET35 throw new SPMeta2NotImplementedException("Not implemented for SP2010 - https://github.com/SubPointSolutions/spmeta2/issues/766"); #endif #if !NET35 if (folderModelHost.CurrentListItem != null) { context.Load(folderModelHost.CurrentListItem, l => l.Folder); } context.ExecuteQueryWithTrace(); serverRelativeUrl = folderModelHost.CurrentListItem == null ? list.RootFolder.ServerRelativeUrl : folderModelHost.CurrentListItem.Folder.ServerRelativeUrl; var currentUrl = serverRelativeUrl + "/" + folderModel.Name; var currentFolder = folderModelHost.CurrentList.ParentWeb.GetFolderByServerRelativeUrl(currentUrl); var doesFolderExist = false; try { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Loading list folder with URL: [{0}]", currentUrl); context.Load(currentFolder, f => f.Name); context.ExecuteQueryWithTrace(); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "List folder with URL does exist: [{0}]", currentUrl); doesFolderExist = true; } catch (ServerException e) { if (e.ServerErrorTypeName == "System.IO.FileNotFoundException") { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "List folder with URL does not exist: [{0}]", currentUrl); doesFolderExist = false; } } if (doesFolderExist) return currentFolder; #endif return null; }
protected bool ShouldDeployLibraryFolder(FolderModelHost folderModelHost) { return folderModelHost.CurrentList != null && folderModelHost.CurrentList.BaseType == BaseType.DocumentLibrary; }
protected Folder GetLibraryFolder(FolderModelHost folderModelHost, FolderDefinition folderModel) { var parentFolder = folderModelHost.CurrentLibraryFolder; var context = parentFolder.Context; context.Load(parentFolder, f => f.Folders); context.ExecuteQueryWithTrace(); // dirty stuff, needs to be rewritten var currentFolder = parentFolder .Folders .OfType<Folder>() .FirstOrDefault(f => f.Name == folderModel.Name); if (currentFolder != null) { context.Load(currentFolder, f => f.Name); context.ExecuteQueryWithTrace(); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Library folder with name does exist: [{0}]", folderModel.Name); } else { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Library folder with name does not exist: [{0}]", folderModel.Name); } return currentFolder; }
private File ProcessFile(FolderModelHost folderHost, ModuleFileDefinition definition) { var context = folderHost.CurrentListFolder.Context; var web = folderHost.CurrentWeb; var list = folderHost.CurrentList; var folder = folderHost.CurrentListFolder; context.Load(folder, f => f.ServerRelativeUrl); #if !NET35 context.Load(folder, f => f.Properties); #endif context.ExecuteQueryWithTrace(); var stringCustomContentType = ResolveContentTypeId(folderHost, definition); if (list != null) { context.Load(list, l => l.EnableMinorVersions); context.Load(list, l => l.EnableVersioning); context.Load(list, l => l.EnableModeration); context.ExecuteQueryWithTrace(); } var file = web.GetFileByServerRelativeUrl(GetSafeFileUrl(folder, definition)); context.Load(file, f => f.Exists); context.ExecuteQueryWithTrace(); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = file.Exists ? file : null, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = folderHost }); #if !NET35 var doesFileHasListItem = //Forms folders !(folder != null && (folder.Properties.FieldValues.ContainsKey("vti_winfileattribs") && folder.Properties.FieldValues["vti_winfileattribs"].ToString() == "00000012")); #endif #if NET35 var doesFileHasListItem = true; #endif WithSafeFileOperation(list, file, f => { var fileName = definition.FileName; var fileContent = definition.Content; var fileCreatingInfo = new FileCreationInformation { Url = fileName, Overwrite = file.Exists }; if (fileContent.Length < ContentStreamFileSize) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Using fileCreatingInfo.Content for small file less than: [{0}]", ContentStreamFileSize); fileCreatingInfo.Content = fileContent; } else { #if NET35 throw new SPMeta2Exception(string.Format("SP2010 CSOM implementation does no support file more than {0}. Checkout FileCreationInformation and avialabe Content size.", ContentStreamFileSize)); #endif #if !NET35 TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Using fileCreatingInfo.ContentStream for big file more than: [{0}]", ContentStreamFileSize); fileCreatingInfo.ContentStream = new System.IO.MemoryStream(fileContent); #endif } TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Overwriting file"); var updatedFile = folder.Files.Add(fileCreatingInfo); FieldLookupService.EnsureDefaultValues(updatedFile.ListItemAllFields, definition.DefaultValues); if (!string.IsNullOrEmpty(stringCustomContentType)) updatedFile.ListItemAllFields[BuiltInInternalFieldNames.ContentTypeId] = stringCustomContentType; if (!string.IsNullOrEmpty(definition.Title)) updatedFile.ListItemAllFields[BuiltInInternalFieldNames.Title] = definition.Title; FieldLookupService.EnsureValues(updatedFile.ListItemAllFields, definition.Values, true); if (!string.IsNullOrEmpty(stringCustomContentType) || definition.DefaultValues.Count > 0 || definition.Values.Count > 0 || !string.IsNullOrEmpty(definition.Title)) { updatedFile.ListItemAllFields.Update(); } return updatedFile; }, doesFileHasListItem); var resultFile = web.GetFileByServerRelativeUrl(GetSafeFileUrl(folder, definition)); context.Load(resultFile, f => f.Exists); context.ExecuteQueryWithTrace(); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = resultFile, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = folderHost }); return resultFile; }
protected string ResolveContentTypeId(FolderModelHost folderHost, ModuleFileDefinition moduleFile) { var context = folderHost.CurrentListFolder.Context; var list = folderHost.CurrentList; var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(moduleFile.ContentTypeId)) { stringCustomContentType = moduleFile.ContentTypeId; } else if (!string.IsNullOrEmpty(moduleFile.ContentTypeName)) { stringCustomContentType = ContentTypeLookupService.LookupContentTypeByName(list, moduleFile.ContentTypeName).Id.ToString(); } return stringCustomContentType; }
private Folder GetListFolder(FolderModelHost folderModelHost, FolderDefinition folderModel, out string serverRelativeUrl) { var list = folderModelHost.CurrentList; var context = list.Context; context.Load(list, l => l.RootFolder); context.Load(list, l => l.ParentWeb); if (folderModelHost.CurrentListItem != null) { context.Load(folderModelHost.CurrentListItem, l => l.Folder); } context.ExecuteQuery(); serverRelativeUrl = folderModelHost.CurrentListItem == null ? list.RootFolder.ServerRelativeUrl : folderModelHost.CurrentListItem.Folder.ServerRelativeUrl; var currentUrl = serverRelativeUrl + "/" + folderModel.Name; var currentFolder = folderModelHost.CurrentList.ParentWeb.GetFolderByServerRelativeUrl(currentUrl); var doesFolderExist = false; try { context.Load(currentFolder, f => f.Name); context.ExecuteQuery(); doesFolderExist = true; } catch (ServerException e) { if (e.ServerErrorTypeName == "System.IO.FileNotFoundException") doesFolderExist = false; } if (doesFolderExist) return currentFolder; return null; }
private ListItem EnsureListFolder(FolderModelHost folderModelHost, FolderDefinition folderModel) { string serverRelativeUrl = string.Empty; var list = folderModelHost.CurrentList; var context = list.Context; var currentFolder = GetListFolder(folderModelHost, folderModel, out serverRelativeUrl); var currentFolderItem = folderModelHost.CurrentListItem; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentFolder, ObjectType = typeof(Folder), ObjectDefinition = folderModel, ModelHost = folderModelHost }); context.ExecuteQuery(); if (currentFolder == null) { currentFolderItem = list.AddItem(new ListItemCreationInformation { FolderUrl = serverRelativeUrl, LeafName = folderModel.Name, UnderlyingObjectType = FileSystemObjectType.Folder }); currentFolderItem["Title"] = folderModel.Name; currentFolderItem.Update(); context.ExecuteQuery(); } else { context.Load(currentFolder, f => f.ListItemAllFields); context.Load(currentFolder, f => f.Name); context.ExecuteQuery(); currentFolderItem = currentFolder.ListItemAllFields; } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentFolder, ObjectType = typeof(Folder), ObjectDefinition = folderModel, ModelHost = folderModelHost }); return currentFolderItem; }
private Folder EnsureLibraryFolder(FolderModelHost folderModelHost, FolderDefinition folderModel) { var parentFolder = folderModelHost.CurrentLibraryFolder; var context = parentFolder.Context; var currentFolder = GetLibraryFolder(folderModelHost, folderModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentFolder, ObjectType = typeof(Folder), ObjectDefinition = folderModel, ModelHost = folderModelHost }); if (currentFolder == null) { currentFolder = parentFolder.Folders.Add(folderModel.Name); context.ExecuteQuery(); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentFolder, ObjectType = typeof(Folder), ObjectDefinition = folderModel, ModelHost = folderModelHost }); currentFolder.Update(); context.ExecuteQuery(); return currentFolder; }
private Folder GetListFolder(FolderModelHost folderModelHost, FolderDefinition folderModel, out string serverRelativeUrl) { var list = folderModelHost.CurrentList; var context = list.Context; context.Load(list, l => l.RootFolder); context.Load(list, l => l.ParentWeb); if (folderModelHost.CurrentListItem != null) { context.Load(folderModelHost.CurrentListItem, l => l.Folder); } context.ExecuteQueryWithTrace(); serverRelativeUrl = folderModelHost.CurrentListItem == null ? list.RootFolder.ServerRelativeUrl : folderModelHost.CurrentListItem.Folder.ServerRelativeUrl; var currentUrl = serverRelativeUrl + "/" + folderModel.Name; var currentFolder = folderModelHost.CurrentList.ParentWeb.GetFolderByServerRelativeUrl(currentUrl); var doesFolderExist = false; try { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Loading list folder with URL: [{0}]", currentUrl); context.Load(currentFolder, f => f.Name); context.ExecuteQueryWithTrace(); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "List folder with URL does exist: [{0}]", currentUrl); doesFolderExist = true; } catch (ServerException e) { if (e.ServerErrorTypeName == "System.IO.FileNotFoundException") { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "List folder with URL does not exist: [{0}]", currentUrl); doesFolderExist = false; } } if (doesFolderExist) return currentFolder; return null; }
protected Folder GetLibraryFolder(FolderModelHost folderModelHost, FolderDefinition folderModel) { return GetLibraryFolder(folderModelHost.CurrentListFolder, folderModel.Name); }
private ListItem EnsureListFolder(FolderModelHost folderModelHost, FolderDefinition folderModel) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "EnsureListFolder()"); var serverRelativeUrl = string.Empty; var list = folderModelHost.CurrentList; var context = list.Context; var currentFolder = GetListFolder(folderModelHost, folderModel, out serverRelativeUrl); var currentFolderItem = folderModelHost.CurrentListItem; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentFolder, ObjectType = typeof(Folder), ObjectDefinition = folderModel, ModelHost = folderModelHost }); context.ExecuteQueryWithTrace(); if (currentFolder == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new list folder"); currentFolderItem = list.AddItem(new ListItemCreationInformation { FolderUrl = serverRelativeUrl, LeafName = folderModel.Name, UnderlyingObjectType = FileSystemObjectType.Folder }); currentFolderItem[BuiltInInternalFieldNames.Title] = folderModel.Name; currentFolderItem.Update(); context.ExecuteQueryWithTrace(); context.Load(currentFolderItem.Folder); context.ExecuteQueryWithTrace(); currentFolder = currentFolderItem.Folder; } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing list folder"); context.Load(currentFolder, f => f.ListItemAllFields); context.Load(currentFolder, f => f.Name); context.ExecuteQueryWithTrace(); currentFolderItem = currentFolder.ListItemAllFields; } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentFolder, ObjectType = typeof(Folder), ObjectDefinition = folderModel, ModelHost = folderModelHost }); return currentFolderItem; }
private void ValidateListFolder(FolderModelHost folderModelHost, FolderDefinition folderModel) { var folder = GetListFolder(folderModelHost, folderModel); ValidateFolderProps(folder, folderModel); }
private Folder EnsureLibraryFolder(FolderModelHost folderModelHost, FolderDefinition folderModel) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "EnsureLibraryFolder()"); var parentFolder = folderModelHost.CurrentLibraryFolder; var context = parentFolder.Context; var currentFolder = GetLibraryFolder(folderModelHost, folderModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentFolder, ObjectType = typeof(Folder), ObjectDefinition = folderModel, ModelHost = folderModelHost }); if (currentFolder == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new library folder"); currentFolder = parentFolder.Folders.Add(folderModel.Name); context.ExecuteQueryWithTrace(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing library folder"); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentFolder, ObjectType = typeof(Folder), ObjectDefinition = folderModel, ModelHost = folderModelHost }); currentFolder.Update(); context.ExecuteQueryWithTrace(); return currentFolder; }
protected File GetFile(FolderModelHost folderHost, ModuleFileDefinition moduleFile) { Folder folder = null; if (folderHost.CurrentList != null) folder = folderHost.CurrentListFolder; else if (folderHost.CurrentContentType != null) folder = folderHost.CurrentContentTypeFolder; else if (folderHost.CurrentWebFolder != null) folder = folderHost.CurrentWebFolder; var web = folderHost.HostWeb; var context = web.Context; var file = web.GetFileByServerRelativeUrl(GetSafeFileUrl(folder, moduleFile)); context.Load(file, f => f.Exists); context.ExecuteQueryWithTrace(); return file; }
public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action<object> action) { var webModelHost = modelHost.WithAssertAndCast<WebModelHost>("modelHost", value => value.RequireNotNull()); var web = webModelHost.HostWeb; var listDefinition = model as ListDefinition; if (web != null && listDefinition != null) { // TODO // no no no no... not a TITLE! var context = web.Context; var list = web.QueryAndGetListByTitle(listDefinition.Title); if (childModelType == typeof(ListViewDefinition)) { context.Load<List>(list, l => l.Views); context.ExecuteQuery(); action(list); } else if (childModelType == typeof(ModuleFileDefinition)) { context.Load<List>(list, l => l.RootFolder); context.Load<List>(list, l => l.BaseType); context.ExecuteQuery(); var folderModelHost = new FolderModelHost { CurrentWeb = web, CurrentList = list, CurrentLibraryFolder = list.RootFolder }; action(folderModelHost); } else if (childModelType == typeof(FolderDefinition)) { context.Load<List>(list, l => l.RootFolder); context.Load<List>(list, l => l.BaseType); context.ExecuteQuery(); var folderModelHost = new FolderModelHost(); folderModelHost.CurrentWeb = web; folderModelHost.CurrentList = list; if (list.BaseType == BaseType.DocumentLibrary) { folderModelHost.CurrentLibraryFolder = list.RootFolder; } else { folderModelHost.CurrentListItem = null; } action(folderModelHost); } else if (childModelType == typeof(SP2013WorkflowSubscriptionDefinition)) { var sp2013WorkflowSubscriptionModelHost = ModelHostBase.Inherit<SP2013WorkflowSubscriptionModelHost>(webModelHost, host => { host.HostList = list; }); action(sp2013WorkflowSubscriptionModelHost); } else { action(list); } list.Update(); } else { action(modelHost); } }
private File ProcessFile(FolderModelHost folderHost, ModuleFileDefinition moduleFile) { var context = folderHost.CurrentLibraryFolder.Context; var web = folderHost.CurrentWeb; var list = folderHost.CurrentList; var folder = folderHost.CurrentLibraryFolder; context.Load(folder, f => f.ServerRelativeUrl); context.ExecuteQuery(); if (list != null) { context.Load(list, l => l.EnableMinorVersions); context.Load(list, l => l.EnableVersioning); context.Load(list, l => l.EnableModeration); context.ExecuteQuery(); } var file = web.GetFileByServerRelativeUrl(GetSafeFileUrl(folder, moduleFile)); context.Load(file, f => f.Exists); context.ExecuteQuery(); InvokeOnModelEvent<ModuleFileDefinition, File>(file, ModelEventType.OnUpdating); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = file.Exists ? file : null, ObjectType = typeof(File), ObjectDefinition = moduleFile, ModelHost = folderHost }); WithSafeFileOperation(list, file, f => { var fileName = moduleFile.FileName; var fileContent = moduleFile.Content; var fileCreatingInfo = new FileCreationInformation { Url = fileName, Content = fileContent, Overwrite = file.Exists }; return folder.Files.Add(fileCreatingInfo); }); var resultFile = web.GetFileByServerRelativeUrl(GetSafeFileUrl(folder, moduleFile)); context.Load(resultFile, f => f.Exists); context.ExecuteQuery(); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = resultFile, ObjectType = typeof(File), ObjectDefinition = moduleFile, ModelHost = folderHost }); InvokeOnModelEvent<ModuleFileDefinition, File>(resultFile, ModelEventType.OnUpdated); return resultFile; }