private static Feature SafelyActivateWebFeature(ClientRuntimeContext context, FeatureCollection features, FeatureDefinition featureModel, Microsoft.SharePoint.Client.FeatureDefinitionScope scope) { var result = features.Add(featureModel.Id, featureModel.ForceActivate, scope); try { context.ExecuteQueryWithTrace(); } catch (Exception e) { // sandbox site/web features? // they need to ne activated with SPFeatureDefinitionScope.Site scope if ((featureModel.Scope == FeatureDefinitionScope.Site || featureModel.Scope == FeatureDefinitionScope.Web) && e.Message.ToUpper().Contains(featureModel.Id.ToString("D").ToUpper())) { result = features.Add(featureModel.Id, featureModel.ForceActivate, Microsoft.SharePoint.Client.FeatureDefinitionScope.Site); context.ExecuteQueryWithTrace(); } else { throw e; } } return(result); }
private Field EnsureField(ClientRuntimeContext context, Field currentField, FieldCollection fieldCollection, FieldDefinition fieldModel) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "EnsureField()"); if (currentField == null) { TraceService.Verbose((int)LogEventId.ModelProvisionProcessingNewObject, "Current field is NULL. Creating new"); var fieldDef = GetTargetSPFieldXmlDefinition(fieldModel); var addFieldOptions = (AddFieldOptions)(int)fieldModel.AddFieldOptions; var resultField = fieldCollection.AddFieldAsXml(fieldDef, fieldModel.AddToDefaultView, addFieldOptions); if (PreloadProperties(resultField)) { context.ExecuteQueryWithTrace(); } ProcessFieldProperties(resultField, fieldModel); return(resultField); } else { TraceService.Verbose((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing field"); ProcessFieldProperties(currentField, fieldModel); return(currentField); } }
protected PropertyValues ExtractProperties(object modelHost) { PropertyValues result = null; ClientRuntimeContext context = null; if (modelHost is SiteModelHost) { result = (modelHost as SiteModelHost).HostSite.RootWeb.AllProperties; context = (modelHost as SiteModelHost).HostSite.RootWeb.Context; } else if (modelHost is WebModelHost) { result = (modelHost as WebModelHost).HostWeb.AllProperties; context = (modelHost as WebModelHost).HostWeb.Context; } else if (modelHost is ListModelHost) { result = (modelHost as ListModelHost).HostList.RootFolder.Properties; context = (modelHost as ListModelHost).HostList.RootFolder.Context; } else if (modelHost is FolderModelHost) { var folderModelHost = modelHost as FolderModelHost; if (folderModelHost.CurrentLibraryFolder != null) { result = folderModelHost.CurrentLibraryFolder.Properties; context = folderModelHost.CurrentLibraryFolder.Context; } else { result = folderModelHost.CurrentListItem.Folder.Properties; context = folderModelHost.CurrentListItem.Context; } } else if (modelHost is ListItem) { // http://officespdev.uservoice.com/forums/224641-general/suggestions/6343086-expose-properties-property-for-microsoft-sharepo throw new SPMeta2NotImplementedException("ListItem properties provision is not supported yet."); //DeployProperty(host, host.CurrentListItem.all, property); } else if (modelHost is File) { // http://officespdev.uservoice.com/forums/224641-general/suggestions/6343087-expose-properties-property-for-microsoft-sharepo throw new SPMeta2NotImplementedException("File properties provision is not supported yet."); // DeployProperty(host, host.CurrentFile., property); } else { throw new SPMeta2NotImplementedException(string.Format("Model host [{0}] is not supported yet.", modelHost)); } context.Load(result); context.ExecuteQueryWithTrace(); return(result); }
private Principal ResolvePrincipal(ClientRuntimeContext context, Web web, string owner) { Principal result = null; var targetSources = new Dictionary <PrincipalType, PrincipalInfo>(); // owner might be only a user or sharepoint group // making a few attempts and checking NULL ref later in the code targetSources.Add(PrincipalType.SharePointGroup, null); targetSources.Add(PrincipalType.User, null); foreach (var targetSource in targetSources.Keys) { // ResolvePrincipal != SearchPrincipals, at all! //var principalInfos = Utility.ResolvePrincipal(context, web, owner, targetSource, PrincipalSource.All, null, false); var principalInfos = Utility.SearchPrincipals(context, web, owner, targetSource, PrincipalSource.All, null, 2); context.ExecuteQueryWithTrace(); if (principalInfos.Count > 0) //if (principalInfos.Value != null) { var info = principalInfos[0]; //var info = principalInfos.Value; targetSources[targetSource] = info; if (targetSource == PrincipalType.User || targetSource == PrincipalType.SecurityGroup) { result = web.EnsureUser(info.LoginName); } if (targetSource == PrincipalType.SharePointGroup) { result = web.SiteGroups.GetById(info.PrincipalId); } context.Load(result); context.ExecuteQueryWithTrace(); // nic, found, break, profit! break; } } return(result); }
protected virtual Group ResolveSecurityGroup(SecurityGroupLinkDefinition securityGroupLinkModel, Web web, ClientRuntimeContext context) { Group securityGroup = null; if (securityGroupLinkModel.IsAssociatedMemberGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedMemberGroup = true. Resolving AssociatedMemberGroup"); context.Load(web, w => w.AssociatedMemberGroup); context.ExecuteQueryWithTrace(); securityGroup = web.AssociatedMemberGroup; } else if (securityGroupLinkModel.IsAssociatedOwnerGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedOwnerGroup = true. Resolving IsAssociatedOwnerGroup"); context.Load(web, w => w.AssociatedOwnerGroup); context.ExecuteQueryWithTrace(); securityGroup = web.AssociatedOwnerGroup; } else if (securityGroupLinkModel.IsAssociatedVisitorGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedVisitorGroup = true. Resolving IsAssociatedVisitorGroup"); context.Load(web, w => w.AssociatedVisitorGroup); context.ExecuteQueryWithTrace(); securityGroup = web.AssociatedVisitorGroup; } else if (!string.IsNullOrEmpty(securityGroupLinkModel.SecurityGroupName)) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving group by name: [{0}]", securityGroupLinkModel.SecurityGroupName); securityGroup = WebExtensions.FindGroupByName(web.SiteGroups, securityGroupLinkModel.SecurityGroupName); } else { TraceService.Error((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedMemberGroup/IsAssociatedOwnerGroup/IsAssociatedVisitorGroup/SecurityGroupName should be defined. Throwing SPMeta2Exception"); throw new SPMeta2Exception("securityGroupLinkModel"); } return(securityGroup); }
private Field EnsureField(ClientRuntimeContext context, Field currentField, FieldCollection fieldCollection, FieldDefinition fieldModel) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "EnsureField()"); if (currentField == null) { TraceService.Verbose((int)LogEventId.ModelProvisionProcessingNewObject, "Current field is NULL. Creating new"); var fieldDef = GetTargetSPFieldXmlDefinition(fieldModel); var addFieldOptions = (AddFieldOptions)(int)fieldModel.AddFieldOptions; var resultField = fieldCollection.AddFieldAsXml(fieldDef, fieldModel.AddToDefaultView, addFieldOptions); if (PreloadProperties(resultField)) { context.ExecuteQueryWithTrace(); } ProcessFieldProperties(resultField, fieldModel); return resultField; } else { TraceService.Verbose((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing field"); ProcessFieldProperties(currentField, fieldModel); return currentField; } }
protected virtual Group ResolveSecurityGroup(SecurityGroupLinkDefinition securityGroupLinkModel, Web web, ClientRuntimeContext context) { Group securityGroup = null; if (securityGroupLinkModel.IsAssociatedMemberGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedMemberGroup = true. Resolving AssociatedMemberGroup"); context.Load(web, w => w.AssociatedMemberGroup); context.ExecuteQueryWithTrace(); securityGroup = web.AssociatedMemberGroup; } else if (securityGroupLinkModel.IsAssociatedOwnerGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedOwnerGroup = true. Resolving IsAssociatedOwnerGroup"); context.Load(web, w => w.AssociatedOwnerGroup); context.ExecuteQueryWithTrace(); securityGroup = web.AssociatedOwnerGroup; } else if (securityGroupLinkModel.IsAssociatedVisitorGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedVisitorGroup = true. Resolving IsAssociatedVisitorGroup"); context.Load(web, w => w.AssociatedVisitorGroup); context.ExecuteQueryWithTrace(); securityGroup = web.AssociatedVisitorGroup; } else if (!string.IsNullOrEmpty(securityGroupLinkModel.SecurityGroupName)) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving group by name: [{0}]", securityGroupLinkModel.SecurityGroupName); securityGroup = WebExtensions.FindGroupByName(web.SiteGroups, securityGroupLinkModel.SecurityGroupName); } else { TraceService.Error((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedMemberGroup/IsAssociatedOwnerGroup/IsAssociatedVisitorGroup/SecurityGroupName should be defined. Throwing SPMeta2Exception"); throw new SPMeta2Exception("securityGroupLinkModel"); } return securityGroup; }
private Principal ResolvePrincipal(ClientRuntimeContext context, Web web, string owner) { Principal result = null; var targetSources = new Dictionary<PrincipalType, PrincipalInfo>(); // owner might be only a user or sharepoint group // making a few attempts and checking NULL ref later in the code targetSources.Add(PrincipalType.SharePointGroup, null); targetSources.Add(PrincipalType.User, null); foreach (var targetSource in targetSources.Keys) { // ResolvePrincipal != SearchPrincipals, at all! //var principalInfos = Utility.ResolvePrincipal(context, web, owner, targetSource, PrincipalSource.All, null, false); var principalInfos = Utility.SearchPrincipals(context, web, owner, targetSource, PrincipalSource.All, null, 2); context.ExecuteQueryWithTrace(); if (principalInfos.Count > 0) //if (principalInfos.Value != null) { var info = principalInfos[0]; //var info = principalInfos.Value; targetSources[targetSource] = info; if (targetSource == PrincipalType.User || targetSource == PrincipalType.SecurityGroup) result = web.EnsureUser(info.LoginName); if (targetSource == PrincipalType.SharePointGroup) result = web.SiteGroups.GetById(info.PrincipalId); context.Load(result); context.ExecuteQueryWithTrace(); // nic, found, break, profit! break; } } return result; }
public override void DeployModel(object modelHost, DefinitionBase model) { this.ModelHost = modelHost; if (!(modelHost is SiteModelHost || modelHost is WebModelHost || modelHost is ListModelHost)) { throw new ArgumentException("modelHost needs to be SiteModelHost/WebModelHost/ListModelHost instance."); } CurrentHostClientContext = (modelHost as CSOMModelHostBase).HostClientContext; CurrentModelHost = modelHost.WithAssertAndCast <CSOMModelHostBase>("modelHost", value => value.RequireNotNull()); HostSite = ExtractSiteFromHost(modelHost); HostWeb = ExtractWebFromHost(modelHost); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Casting field model definition"); var fieldModel = model.WithAssertAndCast <FieldDefinition>("model", value => value.RequireNotNull()); Field currentField = null; ClientRuntimeContext context = null; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = null, ObjectType = GetTargetFieldType(fieldModel), ObjectDefinition = model, ModelHost = modelHost }); if (modelHost is ListModelHost) { var listHost = modelHost as ListModelHost; context = listHost.HostList.Context; currentField = DeployListField(modelHost as ListModelHost, fieldModel); } else if (modelHost is WebModelHost) { var webHost = modelHost as WebModelHost; context = webHost.HostWeb.Context; currentField = DeployWebField(webHost as WebModelHost, fieldModel); } else if (modelHost is SiteModelHost) { var siteHost = modelHost as SiteModelHost; context = siteHost.HostSite.Context; currentField = DeploySiteField(siteHost as SiteModelHost, fieldModel); } else { throw new ArgumentException("modelHost needs to be SiteModelHost/WebModelHost/ListModelHost instance."); } object typedField = null; // emulate context.CastTo<>() call for typed field type if (GetTargetFieldType(fieldModel) != currentField.GetType()) { var targetFieldType = GetTargetFieldType(fieldModel); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Calling context.CastTo() to field type: [{0}]", targetFieldType); var method = context.GetType().GetMethod("CastTo"); var generic = method.MakeGenericMethod(targetFieldType); typedField = generic.Invoke(context, new object[] { currentField }); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = typedField ?? currentField, ObjectType = GetTargetFieldType(fieldModel), ObjectDefinition = model, ModelHost = modelHost }); if (fieldModel.PushChangesToLists.HasValue) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, string.Format("UpdateAndPushChanges({0})", fieldModel.PushChangesToLists)); currentField.UpdateAndPushChanges(fieldModel.PushChangesToLists.Value); } else { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "UpdateAndPushChanges(true)"); // Why does SSOM handler distinguish between list and web/site fields and csom doesn't? currentField.UpdateAndPushChanges(true); } TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); CurrentHostClientContext = null; }
private void ProcessFeature( object modelHost, ClientRuntimeContext context, FeatureCollection features, FeatureDefinition featureModel, Microsoft.SharePoint.Client.FeatureDefinitionScope scope) { var featureId = featureModel.Id; var currentFeature = features.GetById(featureId); features.Context.ExecuteQueryWithTrace(); var featureActivated = IsFeatureActivated(currentFeature); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Is feature activated: [{0}]", featureActivated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentFeature, ObjectType = typeof(Feature), ObjectDefinition = featureModel, ModelHost = modelHost }); if (!featureActivated) { Feature tmpFeature; if (featureModel.Enable) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Enabling feature"); tmpFeature = SafelyActivateWebFeature(context, features, featureModel, scope); } else { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Fetching feature by ID"); tmpFeature = features.GetById(featureId); features.Context.ExecuteQueryWithTrace(); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = tmpFeature, ObjectType = typeof(Feature), ObjectDefinition = featureModel, ModelHost = modelHost }); } else { if (featureModel.Enable && featureModel.ForceActivate) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Feature enabled, but ForceActivate = true. Force activating."); var f = SafelyActivateWebFeature(context, features, featureModel, scope); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = f, ObjectType = typeof(Feature), ObjectDefinition = featureModel, ModelHost = modelHost }); } else if (!featureModel.Enable) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Removing feature."); features.Remove(featureModel.Id, featureModel.ForceActivate); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = null, ObjectType = typeof(Feature), ObjectDefinition = featureModel, ModelHost = modelHost }); context.ExecuteQueryWithTrace(); } else { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Fetching feature by ID"); var tmpFeature = features.GetById(featureId); features.Context.ExecuteQueryWithTrace(); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = tmpFeature, ObjectType = typeof(Feature), ObjectDefinition = featureModel, ModelHost = modelHost }); } } }
public override void DeployModel(object modelHost, DefinitionBase model) { var definition = model.WithAssertAndCast <SearchSettingsDefinition>("model", value => value.RequireNotNull()); Web spObject = null; SearchSettingsConfig searchSettings = null; var searchCenterUrl = String.Empty; ClientRuntimeContext context = null; if (modelHost is SiteModelHost) { spObject = (modelHost as SiteModelHost).HostSite.RootWeb; context = spObject.Context; context.Load(spObject); context.Load(spObject, w => w.AllProperties); context.ExecuteQueryWithTrace(); searchSettings = GetCurrentSearchConfigAtSiteLevel(spObject); searchCenterUrl = GetSearchCenterUrlAtSiteLevel(spObject); } else if (modelHost is WebModelHost) { spObject = (modelHost as WebModelHost).HostWeb; context = spObject.Context; context.Load(spObject); context.Load(spObject, w => w.AllProperties); context.ExecuteQueryWithTrace(); searchSettings = GetCurrentSearchConfigAtWebLevel(spObject); searchCenterUrl = GetSearchCenterUrlAtWebLevel(spObject); } var assert = ServiceFactory.AssertService .NewAssert(definition, spObject) .ShouldNotBeNull(spObject); if (!string.IsNullOrEmpty(definition.SearchCenterUrl)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.SearchCenterUrl); var isValid = s.SearchCenterUrl == searchCenterUrl; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.SearchCenterUrl); } if (!string.IsNullOrEmpty(definition.UseCustomResultsPageUrl)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.UseCustomResultsPageUrl); var isValid = s.UseCustomResultsPageUrl == searchSettings.ResultsPageAddress; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.UseCustomResultsPageUrl); } if (definition.UseParentResultsPageUrl.HasValue) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.UseParentResultsPageUrl); var isValid = s.UseParentResultsPageUrl.Value == searchSettings.Inherit; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.UseParentResultsPageUrl, "UseParentResultsPageUrl is null"); } if (definition.UseFirstSearchNavigationNode.HasValue) { } else { assert.SkipProperty(m => m.UseFirstSearchNavigationNode, "UseFirstSearchNavigationNode is null"); } }
public override void DeployModel(object modelHost, DefinitionBase model) { if (!(modelHost is SiteModelHost || modelHost is ListModelHost)) { throw new ArgumentException("modelHost needs to be SiteModelHost/ListModelHost instance."); } CurrentSiteModelHost = modelHost as SiteModelHost; TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Casting field model definition"); var fieldModel = model.WithAssertAndCast <FieldDefinition>("model", value => value.RequireNotNull()); Field currentField = null; ClientRuntimeContext context = null; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = null, ObjectType = GetTargetFieldType(fieldModel), ObjectDefinition = model, ModelHost = modelHost }); InvokeOnModelEvent <FieldDefinition, Field>(currentField, ModelEventType.OnUpdating); if (modelHost is SiteModelHost) { var siteHost = modelHost as SiteModelHost; context = siteHost.HostSite.Context; currentField = DeploySiteField(siteHost as SiteModelHost, fieldModel); } else if (modelHost is ListModelHost) { var listHost = modelHost as ListModelHost; context = listHost.HostList.Context; currentField = DeployListField(modelHost as ListModelHost, fieldModel); } object typedField = null; // emulate context.CastTo<>() call for typed field type if (GetTargetFieldType(fieldModel) != currentField.GetType()) { var targetFieldType = GetTargetFieldType(fieldModel); TraceService.InformationFormat((int)LogEventId.ModelProvisionCoreCall, "Calling context.CastTo() to field type: [{0}]", targetFieldType); var method = context.GetType().GetMethod("CastTo"); var generic = method.MakeGenericMethod(targetFieldType); typedField = generic.Invoke(context, new object[] { currentField }); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = typedField ?? currentField, ObjectType = GetTargetFieldType(fieldModel), ObjectDefinition = model, ModelHost = modelHost }); InvokeOnModelEvent <FieldDefinition, Field>(currentField, ModelEventType.OnUpdated); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "UpdateAndPushChanges(true)"); currentField.UpdateAndPushChanges(true); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); }
private static Feature SafelyActivateWebFeature(ClientRuntimeContext context, FeatureCollection features, FeatureDefinition featureModel, Microsoft.SharePoint.Client.FeatureDefinitionScope scope) { var result = features.Add(featureModel.Id, featureModel.ForceActivate, scope); try { context.ExecuteQueryWithTrace(); } catch (Exception e) { // sandbox site/web features? // they need to ne activated with SPFeatureDefinitionScope.Site scope if ((featureModel.Scope == FeatureDefinitionScope.Site || featureModel.Scope == FeatureDefinitionScope.Web) && e.Message.ToUpper().Contains(featureModel.Id.ToString("D").ToUpper())) { result = features.Add(featureModel.Id, featureModel.ForceActivate, Microsoft.SharePoint.Client.FeatureDefinitionScope.Site); context.ExecuteQueryWithTrace(); } else { throw e; } } return result; }