private static SPList createListImpl(SPWeb web, string Title, string Description, SPListTemplate template) { web.RequireNotNull("web"); Title.RequireNotNullOrEmpty("Title"); Description.RequireNotNullOrEmpty("Description"); template.RequireNotNull("template"); Guid listGuid = web.Lists.Add(Title, Description, template); return web.Lists[listGuid]; }
public SPList CreateList(SPWeb web, string Title, string Description, SPListTemplate template) { web.RequireNotNull("web"); Title.RequireNotNullOrEmpty("Title"); Description.RequireNotNullOrEmpty("Description"); template.RequireNotNull("template"); SPList list = web.Lists.TryGetList(Title); return null != list ? list : createListImpl(web, Title, Description, template) ; }
public SPFile AddDocumentLink(SPWeb web, SPFolder targetFolder, string documentPath, string documentName, string documentUrl) { web.RequireNotNull("web"); targetFolder.RequireNotNull("targetFolder"); documentPath.RequireNotNullOrEmpty("documentPath"); documentName.RequireNotNullOrEmpty("documentName"); documentUrl.RequireNotNullOrEmpty("documentUrl"); IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent(); IContentTypeOperations contentTypeOps = serviceLocator.GetInstance<IContentTypeOperations>(); string contentTypeName = "Link to a Document"; var contentType = web.AvailableContentTypes[contentTypeName]; SPDocumentLibrary DocLibrary = targetFolder.DocumentLibrary; if (null != DocLibrary) { bool LinkToDocumentApplied = false; foreach (SPContentType cType in DocLibrary.ContentTypes) { if (cType.Name == contentTypeName) { LinkToDocumentApplied = true; break; } } if (!LinkToDocumentApplied) { contentTypeOps.AddContentTypeToList(contentType, DocLibrary); } } var filePath = targetFolder.ServerRelativeUrl; if (!string.IsNullOrEmpty(documentPath)) { filePath += "/" + documentPath; } var currentFolder = web.GetFolder(filePath); var files = currentFolder.Files; var urlOfFile = currentFolder.Url + "/" + documentName + ".aspx"; var builder = new StringBuilder(aspxPageFormat.Length + 400); builder.AppendFormat(aspxPageFormat, typeof(SPDocumentLibrary).Assembly.FullName); var properties = new Hashtable(); properties["ContentTypeId"] = contentType.Id.ToString(); var file = files.Add(urlOfFile, new MemoryStream(new UTF8Encoding().GetBytes(builder.ToString())), properties, false, false); var item = file.Item; item["URL"] = documentUrl + ", "; item.UpdateOverwriteVersion(); return file; }
public static SPFeature ActivateFeatureIfNecessary(SPWeb web, Guid featureGuid) { web.RequireNotNull("web"); featureGuid.Require(Guid.Empty != featureGuid, "featureGuid"); SPFeature feature = web.Features[featureGuid]; if (null == feature) { feature = web.Features.Add(featureGuid); } return feature; }
public void ClearWikiPage(SPFile wikiFile, SPWeb web) { wikiFile.RequireNotNull("wikiFile"); web.RequireNotNull("web"); ChangeWikiContent(wikiFile, string.Empty); using (SPLimitedWebPartManager limitedWebPartManager = wikiFile.GetLimitedWebPartManager(PersonalizationScope.Shared)) { List<Microsoft.SharePoint.WebPartPages.WebPart> webParts = new List<Microsoft.SharePoint.WebPartPages.WebPart>( from Microsoft.SharePoint.WebPartPages.WebPart w in limitedWebPartManager.WebParts select w); webParts.ForEach(w => limitedWebPartManager.DeleteWebPart(w)); } web.Update(); }
public static SPField CreateSiteColumn(SPWeb web, string fieldName, SPFieldType spFieldType, bool required) { // Validation web.RequireNotNull("web"); fieldName.RequireNotNullOrEmpty("fieldName"); if (web.AvailableFields.ContainsField(fieldName)) { return web.AvailableFields[fieldName]; } string internalName = web.Fields.Add(fieldName, spFieldType, required); return web.Fields.GetFieldByInternalName(internalName); }
public static TaxonomyField CreateMangedMetadataSiteColumn(SPWeb web, string fieldName, TermSet termSet, string GroupName) { web.RequireNotNull("web"); fieldName.RequireNotNullOrEmpty("fieldName"); termSet.RequireNotNull("termSet"); if (web.Fields.ContainsField(fieldName)) { return web.Fields[fieldName] as TaxonomyField; } TaxonomyField field = web.Fields.CreateNewField("TaxonomyFieldType", fieldName) as TaxonomyField; field.SspId = termSet.TermStore.Id; field.TermSetId = termSet.Id; field.TargetTemplate = string.Empty; ///field.AllowMultipleValues = true; // field.CreateValuesInEditForm = true; field.Open = true; field.AnchorId = Guid.Empty; field.Group = !string.IsNullOrEmpty(GroupName) ? GroupName : "Managed Metadata"; web.Fields.Add(field); web.Update(); return web.Fields[fieldName] as TaxonomyField; }
public static SPFieldLookup CreateLookupSiteColumn(SPWeb web, SPList list, string title, bool required) { // Validation web.RequireNotNull("web"); list.RequireNotNull("List"); title.RequireNotNullOrEmpty("Title"); if (web.AvailableFields.ContainsField(title)) { return web.AvailableFields[title] as SPFieldLookup; } string internalName = web.Fields.AddLookup(title, list.ID, required); SPFieldLookup lookupField = web.Fields.GetFieldByInternalName(internalName) as SPFieldLookup; return lookupField; }
public static void Unregister(SPWeb web) { web.RequireNotNull("web"); elevatedPrivilegesUnRegisterTypes(web); }
public static SPFeature ActivateFeatureIfNecessary(SPWeb web, Guid featureGuid, bool force, SPFeatureDefinitionScope sPFeatureDefinitionScope) { web.RequireNotNull("web"); featureGuid.Require(Guid.Empty != featureGuid, "featureGuid"); SPFeature feature = web.Features[featureGuid]; if (null == feature || force) { feature = web.Features.Add(featureGuid, force, sPFeatureDefinitionScope); } return feature; }
public static void DeleteList(SPWeb web, string ListTitle) { web.RequireNotNull("web"); ListTitle.RequireNotNullOrEmpty("ListTitle"); SPList list = web.Lists.TryGetList(ListTitle); if (null != list) { web.Lists.Delete(list.ID); } }
public static void AddWorkflow(SPWeb web, SPList list, SPList tasks, SPList workflowHistory, string workflowTemplateName, string workflowName) { // Validation web.RequireNotNull("web"); list.RequireNotNull("list"); tasks.RequireNotNull("tasks"); workflowHistory.RequireNotNull("workflowHistory"); workflowTemplateName.RequireNotNullOrEmpty("builtinWorkflowName"); workflowName.RequireNotNull("workflowName"); SPWorkflowTemplate approvalWorkflow = SharePointUtilities.GetWorkflowByName(web, workflowTemplateName); SPWorkflowAssociation association = SPWorkflowAssociation.CreateListAssociation(approvalWorkflow, workflowName, tasks, workflowHistory); association.AutoStartCreate = true; association.AutoStartChange = true; association.AllowManual = true; list.WorkflowAssociations.Add(association); list.Update(); }
public static void AddPageToNavigation(SPWeb web, SPFile page, string navTitle) { web.RequireNotNull("web"); page.RequireNotNull("page"); navTitle.RequireNotNullOrEmpty("navTitle"); // update the navigation SPNavigationNode node = new SPNavigationNode(navTitle, page.Url); // navigation is shared update the root if (web.ParentWeb.Navigation.UseShared) { using (SPSite site = new SPSite(web.Url)) { SPWeb rootWeb = site.RootWeb; rootWeb.Navigation.TopNavigationBar.AddAsLast(node); } } else { web.Navigation.TopNavigationBar.AddAsLast(node); } }
public static SPContentType TryFindContentType(SPWeb rootWeb, string contentTypeName) { rootWeb.RequireNotNull("rootWeb"); contentTypeName.RequireNotNullOrEmpty("contentTypeName"); SPContentType bestPracticesContentType = (from SPContentType t in rootWeb.ContentTypes where t.Name.Equals(contentTypeName) select t).FirstOrDefault(); return bestPracticesContentType; }
public static SPList CreateWiki(SPWeb web, string title, string description) { web.RequireNotNull("web"); title.RequireNotNullOrEmpty("title"); description.RequireNotNullOrEmpty("description"); SPListTemplate template = web.ListTemplates["Wiki Page Library"]; Guid listID = new Guid(); listID = web.Lists.Add(title, description, template); SPList list = web.Lists[listID]; list.OnQuickLaunch = true; list.Update(); return list; }
public static void DefaultContentApproval(SPWeb web, SPList list, SPList tasks, SPList workflowHistory, string workflowName) { EnableContentApproval(list); const string builtInWorkflowName = "Approval - SharePoint 2010"; web.RequireNotNull("web"); list.RequireNotNull("list"); tasks.RequireNotNull("tasks"); workflowHistory.RequireNotNull("workflowHistory"); workflowName.RequireNotNullOrEmpty("workflowName"); web.AllowUnsafeUpdates = true; SPWorkflowTemplate approvalWorkflow = SharePointUtilities.GetWorkflowByName(web, builtInWorkflowName); SPWorkflowAssociation association = SPWorkflowAssociation.CreateListAssociation(approvalWorkflow, workflowName, tasks, workflowHistory); association.AutoStartCreate = true; association.AutoStartChange = true; association.AllowManual = true; string temp = association.AssociationData; XDocument associationData = XDocument.Parse(temp, LoadOptions.None); association.AssociationData = SharePointUtilities.GetDefaultAssociationData(web, associationData); list.WorkflowAssociations.Add(association); }
public static void AddWorkflow(SPWeb web, SPContentType contentType, SPList tasks, SPList workflowHistory, string workflowTemplateName, string workflowName) { // Validation web.RequireNotNull("web"); contentType.RequireNotNull("list"); tasks.RequireNotNull("tasks"); workflowHistory.RequireNotNull("workflowHistory"); workflowTemplateName.RequireNotNullOrEmpty("workflowTemplateName"); workflowName.RequireNotNull("workflowName"); SPWorkflowTemplate workflowtemplate = SharePointUtilities.GetWorkflowByName(web, workflowTemplateName); SPWorkflowAssociation association = SPWorkflowAssociation.CreateWebContentTypeAssociation(workflowtemplate, workflowName, tasks.Title, workflowHistory.Title); association.AutoStartCreate = true; association.AutoStartChange = true; association.AllowManual = true; if (null == contentType.WorkflowAssociations.GetAssociationByName(association.Name, web.UICulture)) { contentType.WorkflowAssociations.Add(association); } contentType.UpdateWorkflowAssociationsOnChildren(true, // Do not generate full change list true, // Push down to derived content types true, // Push down to list content types false); // Do not throw exception if sealed or readonly }
public static string GetDefaultAssociationData(SPWeb web, XDocument associationData) { web.RequireNotNull("web"); associationData.RequireNotNull("associationData"); SPGroup defaultOwners = web.AssociatedOwnerGroup; XNamespace d = @"http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields"; XNamespace pc = @"http://schemas.microsoft.com/office/infopath/2007/PartnerControls"; XElement person = new XElement(pc + "Person", new XElement(pc + "DisplayName", defaultOwners.Name), new XElement(pc + "AccountId", defaultOwners.LoginName), new XElement(pc + "AccountType", "SharePointGroup") ); associationData.Descendants(d + "Assignee").First().Add(person); associationData.Descendants(d + "AssignmentType").First().Value = "Parallel"; associationData.Descendants(d + "NotificationMessage").First().Value = "Please approve this item!"; var rejection = associationData.Descendants(d + "CancelonRejection").First(); var change = associationData.Descendants(d + "CancelonChange").First(); var enableContentApproval = associationData.Descendants(d + "EnableContentApproval").First(); rejection.Value = change.Value = enableContentApproval.Value = "true"; return associationData.ToString(SaveOptions.DisableFormatting); }
/// <summary> /// AddWorkflow with /// </summary> /// <param name="web"></param> /// <param name="contentType"></param> /// <param name="tasks"></param> /// <param name="workflowHistory"></param> /// <param name="workflowTemplateName"></param> /// <param name="workflowName"></param> /// <param name="associationData"></param> public static void AddWorkflow(SPWeb web, SPContentType contentType, SPList tasks, SPList workflowHistory, string workflowTemplateName, string workflowName, object associationData) { // Validation web.RequireNotNull("web"); contentType.RequireNotNull("list"); tasks.RequireNotNull("tasks"); workflowHistory.RequireNotNull("workflowHistory"); workflowTemplateName.RequireNotNullOrEmpty("workflowTemplateName"); workflowName.RequireNotNull("workflowName"); associationData.RequireNotNull("associationData"); SPWorkflowTemplate workflowtemplate = SharePointUtilities.GetWorkflowByName(web, workflowTemplateName); SPWorkflowAssociation association = SPWorkflowAssociation.CreateWebContentTypeAssociation(workflowtemplate, workflowName, tasks.Title, workflowHistory.Title); association.AutoStartCreate = true; association.AutoStartChange = true; association.AllowManual = true; XmlSerializer serializer = new XmlSerializer(associationData.GetType()); using (MemoryStream stream = new MemoryStream()) { serializer.Serialize(stream, associationData); stream.Position = 0; byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); association.AssociationData = Encoding.UTF8.GetString(bytes); } if (null == contentType.WorkflowAssociations.GetAssociationByName(association.Name, web.UICulture)) { contentType.WorkflowAssociations.Add(association); } contentType.UpdateWorkflowAssociationsOnChildren(true, // Do not generate full change list true, // Push down to derived content types true, // Push down to list content types false); // Do not throw exception if sealed or readonly }
public static SPWorkflowTemplate GetWorkflowByName(SPWeb web, string workflowName) { // validation web.RequireNotNull("web"); workflowName.RequireNotNullOrEmpty("workflowName"); web.AllowUnsafeUpdates = true; SPWorkflowTemplate baseTemplate = (from SPWorkflowTemplate w in web.WorkflowTemplates where w.Name == workflowName select w).FirstOrDefault(); return baseTemplate; }
public static SPFieldChoice CreateChoiceSiteColumn(SPWeb web, string fieldName, IEnumerable<string> choices, bool required) { // Validation web.RequireNotNull("web"); fieldName.RequireNotNullOrEmpty("fieldName"); choices.RequireNotNull("choices"); choices.RequireNotEmpty("choices"); if (web.AvailableFields.ContainsField(fieldName)) { return web.AvailableFields[fieldName] as SPFieldChoice; } string internalName = web.Fields.Add(fieldName, SPFieldType.Choice, required); SPFieldChoice field = web.Fields.GetFieldByInternalName(internalName) as SPFieldChoice; if (null != field) { field.Choices.AddRange(choices.ToArray()); field.Update(); } return field; }
public static SPContentType CreateContentType(SPContentType parentType, SPWeb web, string contentTypeName, string contentTypeGroup, IEnumerable<SPField> fields) { // Validation parentType.RequireNotNull("parentType"); web.RequireNotNull("spContentTypeCollection"); contentTypeName.RequireNotNullOrEmpty("contentTypeName"); contentTypeGroup.RequireNotNullOrEmpty("contentTypeGroup"); fields.RequireNotNull("fields"); SPContentType contentType = web.AvailableContentTypes[contentTypeName]; if (null != contentType) { return contentType; } contentType = new SPContentType(parentType, web.ContentTypes, contentTypeName); contentType = web.ContentTypes.Add(contentType); contentType.Group = contentTypeGroup; foreach (SPField field in fields) { SPFieldLink fieldLink = new SPFieldLink(field); contentType.FieldLinks.Add(fieldLink); } contentType.Update(); return contentType; }
private static void RegisterTypes(SPWeb web) { web.RequireNotNull("web"); LogUtility logger = new LogUtility(); try { logger.TraceDebugInformation("Registering types for use across farms", typeof(ServiceLocationRegistration)); IServiceLocator serviceLocator = new SPWebServiceLocator(web); IServiceLocatorConfig typeMappings = serviceLocator.GetInstance<IServiceLocatorConfig>(); registerTypeMappings(typeMappings); } catch (Exception exception) { logger.TraceDebugException("Exception while registering types!", typeof(ServiceLocationRegistration), exception); } finally { logger.TraceDebugInformation("Finished registering types for use across farms", typeof(ServiceLocationRegistration)); } }