本文整理汇总了C#中Web.GetContentTypeById方法的典型用法代码示例。如果您正苦于以下问题:C# Web.GetContentTypeById方法的具体用法?C# Web.GetContentTypeById怎么用?C# Web.GetContentTypeById使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Web
的用法示例。
在下文中一共展示了Web.GetContentTypeById方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetContentType
public ContentType GetContentType(Web web)
{
if (ContentType != null)
{
return ContentType;
}
ContentType ct;
if (!string.IsNullOrEmpty(Id))
{
ct = web.GetContentTypeById(Id,true);
}
else
{
ct = web.GetContentTypeByName(Name,true);
}
return ct;
}
示例2: CreateList
//.........这里部分代码省略.........
createdList.OnQuickLaunch = list.OnQuickLaunch;
if (createdList.BaseTemplate != (int)ListTemplateType.DiscussionBoard &&
createdList.BaseTemplate != (int)ListTemplateType.Events)
{
createdList.EnableFolderCreation = list.EnableFolderCreation;
}
createdList.Hidden = list.Hidden;
if (createdList.BaseTemplate != (int)ListTemplateType.Survey)
{
createdList.ContentTypesEnabled = list.ContentTypesEnabled;
}
createdList.Update();
web.Context.Load(createdList.Views);
web.Context.Load(createdList, l => l.Id);
web.Context.Load(createdList, l => l.RootFolder.ServerRelativeUrl);
web.Context.Load(createdList.ContentTypes);
web.Context.ExecuteQueryRetry();
if (createdList.BaseTemplate != (int)ListTemplateType.Survey)
{
// Remove existing content types only if there are custom content type bindings
var contentTypesToRemove = new List<ContentType>();
if (list.RemoveExistingContentTypes && list.ContentTypeBindings.Count > 0)
{
contentTypesToRemove.AddRange(createdList.ContentTypes);
}
ContentTypeBinding defaultCtBinding = null;
foreach (var ctBinding in list.ContentTypeBindings)
{
var tempCT = web.GetContentTypeById(ctBinding.ContentTypeId, searchInSiteHierarchy: true);
if (tempCT != null)
{
// Get the name of the existing CT
var name = tempCT.EnsureProperty(ct => ct.Name);
// If the CT does not exist in the target list, and we don't have to remove it
if (!createdList.ContentTypeExistsByName(name) && !ctBinding.Remove)
{
// Then add it to the target list
createdList.AddContentTypeToListById(ctBinding.ContentTypeId, searchContentTypeInSiteHierarchy: true);
}
// Else if the CT exists in the target list, and we have to remove it
else if (createdList.ContentTypeExistsByName(name) && ctBinding.Remove)
{
// Then remove it from the target list
createdList.RemoveContentTypeByName(name);
}
if (ctBinding.Default)
{
defaultCtBinding = ctBinding;
}
}
}
// default ContentTypeBinding should be set last because
// list extension .SetDefaultContentTypeToList() re-sets
// the list.RootFolder UniqueContentTypeOrder property
// which may cause missing CTs from the "New Button"
if (defaultCtBinding != null)
{
createdList.SetDefaultContentTypeToList(defaultCtBinding.ContentTypeId);
示例3: UpdateList
//.........这里部分代码省略.........
{
var existingCustomAction = existingUserCustomActions.AsEnumerable().FirstOrDefault(uca => uca.Name == userCustomAction.Name);
if (existingCustomAction != null)
{
isDirty = true;
// If the custom action already exists
if (userCustomAction.Remove)
{
// And if we need to remove it, we simply delete it
existingCustomAction.DeleteObject();
}
else
{
// Otherwise we update it, and before we force the target
// registration type and ID to avoid issues
userCustomAction.RegistrationType = UserCustomActionRegistrationType.List;
userCustomAction.RegistrationId = existingList.Id.ToString("B").ToUpper();
ObjectCustomActions.UpdateCustomAction(parser, scope, userCustomAction, existingCustomAction);
// Blank out these values again to avoid inconsistent domain model data
userCustomAction.RegistrationType = UserCustomActionRegistrationType.None;
userCustomAction.RegistrationId = null;
}
}
}
}
if (isDirty)
{
existingList.Update();
web.Context.ExecuteQueryRetry();
isDirty = false;
}
}
else
{
scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ListInstances_SkipAddingOrUpdatingCustomActions);
}
#endregion
if (existingList.ContentTypesEnabled)
{
// Check if we need to add a content type
var existingContentTypes = existingList.ContentTypes;
web.Context.Load(existingContentTypes, cts => cts.Include(ct => ct.StringId));
web.Context.ExecuteQueryRetry();
var bindingsToAdd = templateList.ContentTypeBindings.Where(ctb => existingContentTypes.All(ct => !ctb.ContentTypeId.Equals(ct.StringId, StringComparison.InvariantCultureIgnoreCase))).ToList();
var defaultCtBinding = templateList.ContentTypeBindings.FirstOrDefault(ctb => ctb.Default == true);
var currentDefaultContentTypeId = existingContentTypes.First().StringId;
foreach (var ctb in bindingsToAdd)
{
var tempCT = web.GetContentTypeById(ctb.ContentTypeId, searchInSiteHierarchy: true);
if (tempCT != null)
{
// Get the name of the existing CT
var name = tempCT.EnsureProperty(ct => ct.Name);
// If the CT does not exist in the target list, and we don't have to remove it
if (!existingList.ContentTypeExistsByName(name) && !ctb.Remove)
{
existingList.AddContentTypeToListById(ctb.ContentTypeId, searchContentTypeInSiteHierarchy: true);
}
// Else if the CT exists in the target list, and we have to remove it
else if (existingList.ContentTypeExistsByName(name) && ctb.Remove)
{
// Then remove it from the target list
existingList.RemoveContentTypeByName(name);
}
}
}
// default ContentTypeBinding should be set last because
// list extension .SetDefaultContentTypeToList() re-sets
// the list.RootFolder UniqueContentTypeOrder property
// which may cause missing CTs from the "New Button"
if (defaultCtBinding != null)
{
// Only update the defualt contenttype when we detect a change in default value
if (!currentDefaultContentTypeId.Equals(defaultCtBinding.ContentTypeId, StringComparison.InvariantCultureIgnoreCase))
{
existingList.SetDefaultContentTypeToList(defaultCtBinding.ContentTypeId);
}
}
}
if (templateList.Security != null)
{
existingList.SetSecurity(parser, templateList.Security);
}
return Tuple.Create(existingList, parser);
}
else
{
scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ListInstances_List__0____1____2___exists_but_is_of_a_different_type__Skipping_list_, templateList.Title, templateList.Url, existingList.Id);
WriteWarning(string.Format(CoreResources.Provisioning_ObjectHandlers_ListInstances_List__0____1____2___exists_but_is_of_a_different_type__Skipping_list_, templateList.Title, templateList.Url, existingList.Id), ProvisioningMessageType.Warning);
return null;
}
}
示例4: CreateView
private void CreateView(Web web, View view, Microsoft.SharePoint.Client.ViewCollection existingViews, List createdList, PnPMonitoredScope monitoredScope)
{
try
{
var viewElement = XElement.Parse(view.SchemaXml);
var displayNameElement = viewElement.Attribute("DisplayName");
if (displayNameElement == null)
{
throw new ApplicationException("Invalid View element, missing a valid value for the attribute DisplayName.");
}
monitoredScope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstances_Creating_view__0_, displayNameElement.Value);
var existingView = existingViews.FirstOrDefault(v => v.Title == displayNameElement.Value);
if (existingView != null)
{
existingView.DeleteObject();
web.Context.ExecuteQueryRetry();
}
var viewTitle = displayNameElement.Value;
// Type
var viewTypeString = viewElement.Attribute("Type") != null ? viewElement.Attribute("Type").Value : "None";
viewTypeString = viewTypeString[0].ToString().ToUpper() + viewTypeString.Substring(1).ToLower();
var viewType = (ViewType)Enum.Parse(typeof(ViewType), viewTypeString);
// Fields
string[] viewFields = null;
var viewFieldsElement = viewElement.Descendants("ViewFields").FirstOrDefault();
if (viewFieldsElement != null)
{
viewFields = (from field in viewElement.Descendants("ViewFields").Descendants("FieldRef") select field.Attribute("Name").Value).ToArray();
}
// Default view
var viewDefault = viewElement.Attribute("DefaultView") != null && Boolean.Parse(viewElement.Attribute("DefaultView").Value);
// Row limit
var viewPaged = true;
uint viewRowLimit = 30;
var rowLimitElement = viewElement.Descendants("RowLimit").FirstOrDefault();
if (rowLimitElement != null)
{
if (rowLimitElement.Attribute("Paged") != null)
{
viewPaged = bool.Parse(rowLimitElement.Attribute("Paged").Value);
}
viewRowLimit = uint.Parse(rowLimitElement.Value);
}
// Query
var viewQuery = new StringBuilder();
foreach (var queryElement in viewElement.Descendants("Query").Elements())
{
viewQuery.Append(queryElement.ToString());
}
var viewCI = new ViewCreationInformation
{
ViewFields = viewFields,
RowLimit = viewRowLimit,
Paged = viewPaged,
Title = viewTitle,
Query = viewQuery.ToString(),
ViewTypeKind = viewType,
PersonalView = false,
SetAsDefaultView = viewDefault,
};
// Allow to specify a custom view url. View url is taken from title, so we first set title to the view url value we need,
// create the view and then set title back to the original value
var urlAttribute = viewElement.Attribute("Url");
var urlHasValue = urlAttribute != null && !string.IsNullOrEmpty(urlAttribute.Value);
if (urlHasValue)
{
//set Title to be equal to url (in order to generate desired url)
viewCI.Title = Path.GetFileNameWithoutExtension(urlAttribute.Value);
}
var createdView = createdList.Views.Add(viewCI);
web.Context.Load(createdView, v => v.Scope, v => v.JSLink, v => v.Title);
web.Context.ExecuteQueryRetry();
if (urlHasValue)
{
//restore original title
createdView.Title = viewTitle;
createdView.Update();
}
// ContentTypeID
var contentTypeID = viewElement.Attribute("ContentTypeID") != null ? viewElement.Attribute("ContentTypeID").Value : null;
if (!string.IsNullOrEmpty(contentTypeID) && (contentTypeID != BuiltInContentTypeId.System))
{
ContentTypeId childContentTypeId = null;
if (contentTypeID == BuiltInContentTypeId.RootOfList)
{
var childContentType = web.GetContentTypeById(contentTypeID);
//.........这里部分代码省略.........
示例5: CreateList
//.........这里部分代码省略.........
#endif
if (createdList.BaseTemplate == (int)ListTemplateType.DocumentLibrary)
{
// Only supported on Document Libraries
createdList.EnableMinorVersions = list.EnableMinorVersions;
createdList.DraftVersionVisibility = (DraftVisibilityType)list.DraftVersionVisibility;
if (list.EnableMinorVersions)
{
createdList.MajorWithMinorVersionsLimit = list.MinorVersionLimit; // Set only if enabled, otherwise you'll get exception due setting value to zero.
// DraftVisibilityType.Approver is available only when the EnableModeration option of the list is true
if (DraftVisibilityType.Approver ==
(DraftVisibilityType)list.DraftVersionVisibility)
{
if (list.EnableModeration)
{
createdList.DraftVersionVisibility =
(DraftVisibilityType)list.DraftVersionVisibility;
}
else
{
scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ListInstances_DraftVersionVisibility_not_applied_because_EnableModeration_is_not_set_to_true);
WriteWarning(CoreResources.Provisioning_ObjectHandlers_ListInstances_DraftVersionVisibility_not_applied_because_EnableModeration_is_not_set_to_true, ProvisioningMessageType.Warning);
}
}
else
{
createdList.DraftVersionVisibility = (DraftVisibilityType)list.DraftVersionVisibility;
}
}
}
}
}
createdList.OnQuickLaunch = list.OnQuickLaunch;
if (createdList.BaseTemplate != (int)ListTemplateType.DiscussionBoard)
{
createdList.EnableFolderCreation = list.EnableFolderCreation;
}
createdList.Hidden = list.Hidden;
createdList.ContentTypesEnabled = list.ContentTypesEnabled;
createdList.Update();
web.Context.Load(createdList.Views);
web.Context.Load(createdList, l => l.Id);
web.Context.Load(createdList, l => l.RootFolder.ServerRelativeUrl);
web.Context.Load(createdList.ContentTypes);
web.Context.ExecuteQueryRetry();
// Remove existing content types only if there are custom content type bindings
var contentTypesToRemove = new List<ContentType>();
if (list.RemoveExistingContentTypes && list.ContentTypeBindings.Count > 0)
{
contentTypesToRemove.AddRange(createdList.ContentTypes);
}
ContentTypeBinding defaultCtBinding = null;
foreach (var ctBinding in list.ContentTypeBindings)
{
var tempCT = web.GetContentTypeById(ctBinding.ContentTypeId, searchInSiteHierarchy: true);
if (tempCT != null)
{
// Check if CT is already available
var name = tempCT.EnsureProperty(ct => ct.Name);
if (!createdList.ContentTypeExistsByName(name))
{
createdList.AddContentTypeToListById(ctBinding.ContentTypeId, searchContentTypeInSiteHierarchy: true);
}
if (ctBinding.Default)
{
defaultCtBinding = ctBinding;
}
}
}
// default ContentTypeBinding should be set last because
// list extension .SetDefaultContentTypeToList() re-sets
// the list.RootFolder UniqueContentTypeOrder property
// which may cause missing CTs from the "New Button"
if (defaultCtBinding != null)
{
createdList.SetDefaultContentTypeToList(defaultCtBinding.ContentTypeId);
}
// Effectively remove existing content types, if any
foreach (var ct in contentTypesToRemove)
{
ct.DeleteObject();
web.Context.ExecuteQueryRetry();
}
if (list.Security != null)
{
createdList.SetSecurity(parser, list.Security);
}
return Tuple.Create(createdList, parser);
}
示例6: BindFieldsToContentType
private void BindFieldsToContentType(Web web, string contentTypeId, IEnumerable<Field> fields) {
var contentType = web.GetContentTypeById(contentTypeId);
web.Context.Load(web.Fields);
web.Context.Load(contentType.FieldLinks);
web.Context.ExecuteQuery();
var missingFields = fields.Except(from f in fields
join fl in contentType.FieldLinks on f.StaticName equals fl.Name
select f);
foreach (var field in missingFields) {
var fieldLinkCreationInfo = new FieldLinkCreationInformation() {
Field = field
};
contentType.FieldLinks.Add(fieldLinkCreationInfo);
}
contentType.Update(false);
contentType.Context.ExecuteQuery();
}
示例7: GetDocumentSetTemplate
public DocumentSetTemplate GetDocumentSetTemplate(Web web)
{
if (_contentType != null)
{
var docSet = DocumentSetTemplate.GetDocumentSetTemplate(web.Context, _contentType);
return docSet;
}
else if (_documentSetTemplate != null)
{
return _documentSetTemplate;
}
else
{
ContentType ct;
if (!string.IsNullOrEmpty(Id))
{
ct = web.GetContentTypeById(Id, true);
}
else
{
ct = web.GetContentTypeByName(Name, true);
}
_contentType = ct;
var docSet = DocumentSetTemplate.GetDocumentSetTemplate(web.Context, ct);
return docSet;
}
}
示例8: BindFieldsToContentType
private static void BindFieldsToContentType(Web web, string contentTypeId, IEnumerable<Field> fields)
{
var contentType = web.GetContentTypeById(contentTypeId);
var descriptionField = web.Fields.GetByInternalNameOrTitle(FIELD_DESCRIPTION);
web.Context.Load(web.Fields);
web.Context.Load(descriptionField);
web.Context.Load(contentType.FieldLinks);
web.Context.ExecuteQuery();
// add the description field
((List<Field>)fields).Insert(0, descriptionField);
// add any fields which have not be included
var missingFields = fields.Except(from f in fields
join fl in contentType.FieldLinks on f.StaticName equals fl.Name
select f).ToList();
foreach (var field in missingFields)
{
var fieldLinkCreationInfo = new FieldLinkCreationInformation()
{
Field = field
};
contentType.FieldLinks.Add(fieldLinkCreationInfo);
}
contentType.Update(false);
contentType.Context.ExecuteQuery();
}