internal static TermStore FindTermStore(SPSite site, string termStorename, Guid? termStoreId, bool? useDefaultSiteCollectionTermStore) { var session = new TaxonomySession(site); TermStore termStore = null; if (useDefaultSiteCollectionTermStore.HasValue && useDefaultSiteCollectionTermStore.Value == true) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Resolving Term Store as useDefaultSiteCollectionTermStore"); termStore = session.DefaultSiteCollectionTermStore; } else if (termStoreId.HasGuidValue()) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving Term Store by ID: [{0}]", termStoreId.Value); termStore = session.TermStores[termStoreId.Value]; } else if (!string.IsNullOrEmpty(termStorename)) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving Term Store by Name: [{0}]", termStorename); termStore = session.TermStores[termStorename]; } return termStore; }
public SPWeb GetTargetWeb(SPSite site, string webUrl, Guid? webId) { if (webId.HasGuidValue()) { return site.OpenWeb(webId.Value); } else if (!string.IsNullOrEmpty(webUrl)) { var targetWebUrl = TokenReplacementService.ReplaceTokens(new TokenReplacementContext { Value = webUrl, Context = site }).Value; // server relative URl, always targetWebUrl = UrlUtility.RemoveStartingSlash(targetWebUrl); targetWebUrl = "/" + targetWebUrl; var targetWeb = site.OpenWeb(targetWebUrl); return targetWeb; } // root web by default return site.RootWeb; }
public Web GetTargetWeb(Site site, string webUrl, Guid? webId) { var context = site.Context; if (webId.HasGuidValue()) { var targetWeb = site.OpenWebById(webId.Value); context.Load(targetWeb); context.ExecuteQueryWithTrace(); return targetWeb; } else if (!string.IsNullOrEmpty(webUrl)) { var oldValue = CSOMTokenReplacementService.AllowClientContextAsTokenReplacementContext; try { CSOMTokenReplacementService.AllowClientContextAsTokenReplacementContext = true; var targetWebUrl = TokenReplacementService.ReplaceTokens(new TokenReplacementContext { Value = webUrl, Context = context }).Value; // server relative url, ensure / in the beginning targetWebUrl = UrlUtility.RemoveStartingSlash(targetWebUrl); targetWebUrl = "/" + targetWebUrl; var targetWeb = site.OpenWeb(targetWebUrl); context.Load(targetWeb); context.ExecuteQueryWithTrace(); return targetWeb; } finally { CSOMTokenReplacementService.AllowClientContextAsTokenReplacementContext = oldValue; } } // root web by default return site.RootWeb; }
public static List LookupList(Web web, string listUrl, string listTitle, Guid? listId) { List list = null; if (!string.IsNullOrEmpty(listUrl)) { list = web.QueryAndGetListByUrl(listUrl); } else if (!string.IsNullOrEmpty(listTitle)) { list = web.Lists.GetByTitle(listTitle); } else if (listId.HasGuidValue()) { list = web.Lists.GetById(listId.Value); } else { throw new SPMeta2Exception("ListUrl, ListTitle or ListId should be defined."); } return list; }
public static TermSet LookupTermSet(ClientRuntimeContext context, TermStore termStore, Site site, string termGroupName, Guid? termGroupId, bool? isSiteCollectionGroup, string termSetName, Guid? termSetId, int termSetLCID) { var storeContext = context; TermGroup currenGroup = null; if (!string.IsNullOrEmpty(termGroupName)) { currenGroup = termStore.Groups.GetByName(termGroupName); storeContext.Load(currenGroup); storeContext.ExecuteQueryWithTrace(); } else if (termGroupId != null && termGroupId.HasGuidValue()) { currenGroup = termStore.Groups.GetById(termGroupId.Value); storeContext.Load(currenGroup); storeContext.ExecuteQueryWithTrace(); } else if (isSiteCollectionGroup == true) { currenGroup = termStore.GetSiteCollectionGroup(site, false); storeContext.Load(currenGroup); storeContext.ExecuteQueryWithTrace(); } if (!string.IsNullOrEmpty(termSetName)) { if (currenGroup != null && (currenGroup.ServerObjectIsNull == false)) { TermSet termSet = null; var scope = new ExceptionHandlingScope(storeContext); using (scope.StartScope()) { using (scope.StartTry()) { termSet = currenGroup.TermSets.GetByName(termSetName); storeContext.Load(termSet); } using (scope.StartCatch()) { } } storeContext.ExecuteQueryWithTrace(); if (termSet != null && termSet.ServerObjectIsNull == false) { storeContext.Load(termSet, g => g.Id); storeContext.ExecuteQueryWithTrace(); return termSet; } } else { var termSets = termStore.GetTermSetsByName(termSetName, termSetLCID); storeContext.Load(termSets); storeContext.ExecuteQueryWithTrace(); return termSets.FirstOrDefault(); } } if (termSetId.HasGuidValue()) { if (currenGroup != null && (currenGroup.ServerObjectIsNull == false)) { TermSet termSet = null; var scope = new ExceptionHandlingScope(storeContext); using (scope.StartScope()) { using (scope.StartTry()) { termSet = currenGroup.TermSets.GetById(termSetId.Value); storeContext.Load(termSet); } using (scope.StartCatch()) { } } storeContext.ExecuteQueryWithTrace(); if (termSet != null && termSet.ServerObjectIsNull == false) { storeContext.Load(termSet, g => g.Id); storeContext.ExecuteQueryWithTrace(); return termSet; } } else { TermSet termSet = null; var scope = new ExceptionHandlingScope(storeContext); using (scope.StartScope()) { using (scope.StartTry()) { termSet = termStore.GetTermSet(termSetId.Value); storeContext.Load(termSet); } using (scope.StartCatch()) { } } storeContext.ExecuteQueryWithTrace(); if (termSet != null && termSet.ServerObjectIsNull == false) { storeContext.Load(termSet, g => g.Id); storeContext.ExecuteQueryWithTrace(); return termSet; } } } return null; }
public static TermSet LookupTermSet( TermStore tesmStore, string termGroupName, Guid? groupId, bool? isSiteCollectionGroup, SPSite site, string termSetName, Guid? termSetId, int termSetLCID) { Group currentGroup = null; if (!string.IsNullOrEmpty(termGroupName)) { currentGroup = tesmStore.Groups.FirstOrDefault(g => g.Name.ToUpper() == termGroupName.ToUpper()); } else if (groupId != null && groupId.HasGuidValue()) { currentGroup = tesmStore.GetGroup(groupId.Value); } else if (isSiteCollectionGroup.HasValue && isSiteCollectionGroup.Value) { currentGroup = tesmStore.GetSiteCollectionGroup(site); } if (termSetId.HasGuidValue()) { if (currentGroup != null) return currentGroup.TermSets.FirstOrDefault(t => t.Id == termSetId.Value); return tesmStore.GetTermSet(termSetId.Value); } if (!string.IsNullOrEmpty(termSetName)) { if (currentGroup != null) return currentGroup.TermSets.FirstOrDefault(t => t.Name.ToUpper() == termSetName.ToUpper()); return tesmStore.GetTermSets(termSetName, termSetLCID).FirstOrDefault(); } return null; }
internal static ListBindContext LookupBindContext(ListItemModelHost listItemModelHost, string webUrl, Guid? webId, string listUrl, string listTitle, Guid? listId, string viewTitle, Guid? viewId, string webPartTitleUrl ) { var result = new ListBindContext { }; var web = listItemModelHost.HostWeb; var context = listItemModelHost.HostWeb.Context; var targetWeb = listItemModelHost.HostWeb; if (webId.HasGuidValue() || !string.IsNullOrEmpty(webUrl)) { targetWeb = new LookupFieldModelHandler() .GetTargetWeb(listItemModelHost.HostClientContext.Site, webUrl, webId); } var list = LookupList(targetWeb, listUrl, listTitle, listId); View view = null; if (viewId.HasValue && viewId != default(Guid)) view = list.Views.GetById(viewId.Value); else if (!string.IsNullOrEmpty(viewTitle)) view = list.Views.GetByTitle(viewTitle); context.Load(list, l => l.Id); context.Load(list, l => l.DefaultViewUrl); context.Load(list, l => l.Title); // TODO, https://github.com/SubPointSolutions/spmeta2/issues/765 // list.DefaultView is not available, so a full fetch for list view is a must for SP2010. #if !NET35 context.Load(list, l => l.DefaultView); #endif if (view != null) { context.Load(view); context.ExecuteQueryWithTrace(); #if !NET35 result.OriginalView = list.DefaultView; result.OriginalViewId = list.DefaultView.Id; #endif result.TargetView = view; result.TargetViewId = view.Id; result.TitleUrl = view.ServerRelativeUrl; } else { context.ExecuteQueryWithTrace(); } result.ListId = list.Id; result.List = list; if (webPartTitleUrl == null) { if (string.IsNullOrEmpty(result.TitleUrl)) result.TitleUrl = list.DefaultViewUrl; } #if !NET35 result.DefaultViewId = list.DefaultView.Id; #endif return result; }
private void HandleWikiPageProvision(ListItem listItem, WebPartDefinitionBase webpartModel, Guid? currentWebPartStoreKey, Guid? oldWebParStoreKey) { if (!webpartModel.AddToPageContent) return; TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "AddToPageContent = true. Handling wiki/publishig page provision case."); var context = listItem.Context; var targetFieldName = string.Empty; if (listItem.FieldValues.ContainsKey(BuiltInInternalFieldNames.WikiField)) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "WikiField field is detected. Switching to wiki page web part provision."); targetFieldName = BuiltInInternalFieldNames.WikiField; } else if (listItem.FieldValues.ContainsKey(BuiltInInternalFieldNames.PublishingPageLayout)) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "PublishingPageLayout field is detected. Switching to publishin page web part provision."); targetFieldName = BuiltInInternalFieldNames.PublishingPageContent; } else { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "Not PublishingPageLayout field, nor WikiField is detected. Skipping."); return; } // any on the page? var existingWebPartId = string.Empty; // current from the new provision var upcomingWebPartId = string.Empty; // weird, by some web part ignor ID from the XML // so webpartStoreKey from the previous CSOM adding web part to the page must be used // M2 covers that fact with the regression testing, so we know what are they // and we have NOD idea why it happens if (ShouldUseWebPartStoreKeyForWikiPage) { upcomingWebPartId = currentWebPartStoreKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); ; } else { // get from the model upcomingWebPartId = webpartModel.Id.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); ; } if (!oldWebParStoreKey.HasGuidValue()) { // first provision existingWebPartId = currentWebPartStoreKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); } else { // second provision, // we had web part on the page and can reuse that ID to relink on wiki content existingWebPartId = oldWebParStoreKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); } var content = listItem[targetFieldName] == null ? string.Empty : listItem[targetFieldName].ToString(); var wikiTemplate = new StringBuilder(); // actual ID will be replaced later wikiTemplate.AppendFormat("<div class='ms-rtestate-read ms-rte-wpbox' contentEditable='false'>"); wikiTemplate.Append(" <div class='ms-rtestate-read {0}' id='div_{0}'>"); wikiTemplate.AppendFormat(" </div>"); wikiTemplate.AppendFormat("</div>"); var wikiTemplateOutput = wikiTemplate.ToString(); if (string.IsNullOrEmpty(content)) { // page is empty, pre-generating HTML // pushing web part as the current WebPart Key TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Page content is empty Generating new one."); content = string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // there is a content on the page // there might be some web parts too if (oldWebParStoreKey.HasGuidValue()) { // there was an old web part on the page // checking if markup has the ID if (content.ToUpper().IndexOf(existingWebPartId.ToUpper()) != -1) { // was old web part on the page? // yes, replacing ID TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, string.Format("Replacing web part with ID: [{0}] to [{1}] on the page content.", existingWebPartId, upcomingWebPartId), null); content = content.Replace(existingWebPartId, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } // original from the definigion? else if (content.ToUpper().IndexOf(upcomingWebPartId.ToUpper()) != -1) { // yes, replacing ID TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, string.Format("Replacing web part with ID: [{0}] to [{1}] on the page content.", existingWebPartId, upcomingWebPartId), null); // do nothing listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // adding new, from scratch TraceService.WarningFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find web part ID: [{0}] the page content. Adding a new one.", new object[] { existingWebPartId }); content = content + string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } } else { // first provision of the web part on the page if (content.ToUpper().IndexOf(upcomingWebPartId.ToUpper()) != -1) { // do nothing listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // adding new, from scratch TraceService.WarningFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find web part ID: [{0}] the page content. Adding a new one.", new object[] { existingWebPartId }); content = content + string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } } } }
public static TermSet LookupTermSet(ClientRuntimeContext context, TermStore termStore, string termSetName, Guid? termSetId, int termSetLCID) { var storeContext = context; if (!string.IsNullOrEmpty(termSetName)) { var termSets = termStore.GetTermSetsByName(termSetName, termSetLCID); storeContext.Load(termSets); storeContext.ExecuteQueryWithTrace(); return termSets.FirstOrDefault(); } if (termSetId.HasGuidValue()) { TermSet termSet = null; var scope = new ExceptionHandlingScope(storeContext); using (scope.StartScope()) { using (scope.StartTry()) { termSet = termStore.GetTermSet(termSetId.Value); storeContext.Load(termSet); } using (scope.StartCatch()) { } } storeContext.ExecuteQueryWithTrace(); if (termSet != null && termSet.ServerObjectIsNull == false) { storeContext.Load(termSet, g => g.Id); storeContext.ExecuteQueryWithTrace(); return termSet; } } return null; }
private void HandleWikiPageProvision(ListItem listItem, WebPartDefinitionBase webpartModel, Guid? webPartStoreKey, Guid? oldWebParKey) { if (!webpartModel.AddToPageContent) return; TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "AddToPageContent = true. Handling wiki/publishig page provision case."); var context = listItem.Context; var targetFieldName = string.Empty; if (listItem.FieldValues.ContainsKey(BuiltInInternalFieldNames.WikiField)) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "WikiField field is detected. Switching to wiki page web part provision."); targetFieldName = BuiltInInternalFieldNames.WikiField; } else if (listItem.FieldValues.ContainsKey(BuiltInInternalFieldNames.PublishingPageLayout)) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "PublishingPageLayout field is detected. Switching to publishin page web part provision."); targetFieldName = BuiltInInternalFieldNames.PublishingPageContent; } else { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "Not PublishingPageLayout field, nor WikiField is detected. Skipping."); return; } var wikiTemplate = new StringBuilder(); var existingWebPartId = string.Empty; var definitionWebPartId = webpartModel.Id.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); ; var upcomingWebPartId = definitionWebPartId; // aa.... // extremely unfortunate if (webpartModel is XsltListViewWebPartDefinition) { upcomingWebPartId = webPartStoreKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); ; } if (!oldWebParKey.HasGuidValue()) { // first provision existingWebPartId = webPartStoreKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); } else { // second, so that we had web part and use that ID existingWebPartId = oldWebParKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); } var content = listItem[targetFieldName] == null ? string.Empty : listItem[targetFieldName].ToString(); // actual ID will be replaced later wikiTemplate.AppendFormat("<div class='ms-rtestate-read ms-rte-wpbox' contentEditable='false'>"); wikiTemplate.Append(" <div class='ms-rtestate-read {0}' id='div_{0}'>"); wikiTemplate.AppendFormat(" </div>"); wikiTemplate.AppendFormat("</div>"); var wikiTemplateOutput = wikiTemplate.ToString(); if (string.IsNullOrEmpty(content)) { // page is empty, pre-generating HTML // pushing web part as the current WebPart Key TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Page content is empty Generating new one."); content = string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // we had web part on the page // so we need to change the ID if (oldWebParKey.HasGuidValue()) { // was old on the page? if (content.ToUpper().IndexOf(existingWebPartId.ToUpper()) != -1) { // yes, replacing ID TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, string.Format("Replacing web part with ID: [{0}] to [{1}] on the page content.", existingWebPartId, upcomingWebPartId), null); content = content.Replace(existingWebPartId, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } // original from the definigion? else if (content.ToUpper().IndexOf(definitionWebPartId.ToUpper()) != -1) { // yes, replacing ID TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, string.Format("Replacing web part with ID: [{0}] to [{1}] on the page content.", existingWebPartId, upcomingWebPartId), null); content = content.Replace(definitionWebPartId, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // adding new, from scratch TraceService.WarningFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find web part ID: [{0}] the page content. Adding a new one.", new object[] { existingWebPartId }); content = string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } } else { // first provision, no web parts on the wiki page // there should be a definition based web part id in the template // updatting to a upcoming ID if (content.ToUpper().IndexOf(definitionWebPartId.ToUpper()) != -1) { // yes, replacing ID TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, string.Format("Replacing web part with ID: [{0}] to [{1}] on the page content.", definitionWebPartId, upcomingWebPartId), null); content = content.Replace(definitionWebPartId, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // adding new, from scratch TraceService.WarningFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find web part ID: [{0}] the page content. Adding a new one.", new object[] { existingWebPartId }); content = string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } } } }
internal static ListBindContext LookupBindContext(ListItemModelHost listItemModelHost, string webUrl, Guid? webId, string listUrl, string listTitle, Guid? listId, string viewTitle, Guid? viewId, string webPartTitleUrl ) { var result = new ListBindContext { }; var web = listItemModelHost.HostWeb; var context = listItemModelHost.HostWeb.Context; var targetWeb = listItemModelHost.HostWeb; if (webId.HasGuidValue() || !string.IsNullOrEmpty(webUrl)) { targetWeb = new LookupFieldModelHandler() .GetTargetWeb(listItemModelHost.HostClientContext.Site, webUrl, webId); } var list = LookupList(targetWeb, listUrl, listTitle, listId); View view = null; if (viewId.HasValue && viewId != default(Guid)) view = list.Views.GetById(viewId.Value); else if (!string.IsNullOrEmpty(viewTitle)) view = list.Views.GetByTitle(viewTitle); context.Load(list, l => l.Id); context.Load(list, l => l.DefaultViewUrl); context.Load(list, l => l.Title); context.Load(list, l => l.DefaultView); if (view != null) { context.Load(view); context.ExecuteQueryWithTrace(); result.OriginalView = list.DefaultView; result.OriginalViewId = list.DefaultView.Id; result.TargetView = view; result.TargetViewId = view.Id; result.TitleUrl = view.ServerRelativeUrl; } else { context.ExecuteQueryWithTrace(); } result.ListId = list.Id; result.List = list; if (webPartTitleUrl == null) { if (string.IsNullOrEmpty(result.TitleUrl)) result.TitleUrl = list.DefaultViewUrl; } result.DefaultViewId = list.DefaultView.Id; return result; }
public static TermSet LookupTermSet(TermStore tesmStore, string termSetName, Guid? termSetId, int termSetLCID) { if (termSetId.HasGuidValue()) return tesmStore.GetTermSet(termSetId.Value); if (!string.IsNullOrEmpty(termSetName)) return tesmStore.GetTermSets(termSetName, termSetLCID).FirstOrDefault(); return null; }