public SPFieldLookup CreateLookup(SPFieldCollection siteColumns, string fieldName, SPList lookupList, SPWeb web, bool required)
 {
     siteColumns.RequireNotNull("siteColumns");
     fieldName.RequireNotNullOrEmpty("fieldName");
     lookupList.RequireNotNull("lookupList");
     string internalFieldName;
     SPField looupField = TryGetField(siteColumns,fieldName);
     if (null == looupField)
     {
         if (null != web)
         {
             internalFieldName = siteColumns.AddLookup(fieldName, lookupList.ID, web.ID, required);
         }
         else
         {
             internalFieldName = siteColumns.AddLookup(fieldName, lookupList.ID, required);
         }
         looupField = siteColumns.GetFieldByInternalName(internalFieldName);
     }
     return looupField as SPFieldLookup;
 }
        public XsltListViewWebPart AddListToPage(SPList list, string title, string zone, SPLimitedWebPartManager webPartManager, int index)
        {
            // validation
            list.RequireNotNull("list");
            title.RequireNotNullOrEmpty("title");
            zone.RequireNotNullOrEmpty("zone");
            webPartManager.RequireNotNull("webPartManager");
            index.Require(index >= 0, "index");

            XsltListViewWebPart wp = new XsltListViewWebPart();
            wp.ListName = list.ID.ToString("B").ToUpper();
            wp.Title = title;
            wp.ZoneID = zone;
            ModifyViewClass viewOperations = new ModifyViewClass();
            SPView defaultView = viewOperations.GetDefaultView(list);
            SPView modifiedView = viewOperations.CopyView(defaultView, list);
            viewOperations.SetToolbarType(modifiedView, "Standard");
            modifiedView.Update();
            wp.ViewGuid = modifiedView.ID.ToString("B").ToUpper();
            webPartManager.AddWebPart(wp, zone, index);
            list.Update();
            webPartManager.SaveChanges(wp);
            return wp;
        }
        public static SPView GetDefaultView(SPList list)
        {
            list.RequireNotNull("list");

            SPView listView = (from SPView v in list.Views
                               where v.DefaultView == true
                               select v).First();

            return listView;
        }
 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 SPFile CreatePage(SPList list, string fileName, SPTemplateFileType fileType)
 {
     list.RequireNotNull("list");
     fileName.RequireNotNullOrEmpty("fileName");
     return list.RootFolder.Files.Add(string.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fileName), fileType);
 }
        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 ChangeTitleDisplayName(SPList list, string newTitle)
        {
            list.RequireNotNull("list");
            newTitle.RequireNotNullOrEmpty("newTitle");

            SPField titleField = list.Fields.TryGetFieldByStaticName("Title");
            if (null != titleField)
            {
                titleField.Title = newTitle;
                titleField.Update();
            }
        }
        /// <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
        }
 /// <summary>
 /// Writes messages of 255 characters or less
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="list">The list.</param>
 private void WriteMessage(string message, SPList list)
 {
     message.RequireNotNullOrEmpty("message");
     list.RequireNotNull("list");
     message.Require(message.Length <= 255, "message");
     SPListItem item = list.AddItem();
     item["Title"] = message;
     item.Update();
 }
        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 AddListToPage(SPList list, string title, string zone, SPLimitedWebPartManager webPartManager, int index, string viewName)
        {
            // validation
            list.RequireNotNull("list");
            title.RequireNotNullOrEmpty("title");
            webPartManager.RequireNotNull("webPartManager");
            viewName.RequireNotNullOrEmpty("viewName");

            ListViewWebPart wp = new ListViewWebPart();
            wp.ListName = list.ID.ToString("B").ToUpper();
            wp.ViewGuid = list.Views[viewName].ID.ToString("B").ToUpper();
            wp.Title = title;
            wp.ZoneID = zone;
            webPartManager.AddWebPart(wp, zone, index);
            list.Update();
            webPartManager.SaveChanges(wp);
        }
        public static void AddFieldToList(SPList list, IEnumerable<SPField> fields)
        {
            list.RequireNotNull("list");
            fields.RequireNotNull("field");

            foreach (SPField field in fields)
            {
                if (null != field && !list.Fields.Contains(field.Id))
                {
                    list.Fields.Add(field);
                }
            }

            list.Update();
        }
        public static void AddFieldToList(SPList list, SPField field)
        {
            list.RequireNotNull("list");
            field.RequireNotNull("field");

            if (!list.Fields.Contains(field.Id))
            {
                list.Fields.Add(field);
                list.Update();
            }
        }
        public static void MakeDefaultContentType(SPList list, SPContentType contentType)
        {
            //Validation
            list.RequireNotNull("list");
            contentType.RequireNotNull("contentType");

            SPContentType existingContentType = list.ContentTypes[contentType.Name];
            if (null == existingContentType)
            {
                list.ContentTypes.Add(contentType);
                list.Update();
            }

            List<SPContentType> cts = new List<SPContentType>();

            foreach (SPContentType item in list.ContentTypes)
            {
                if (item.Name.Equals(contentType.Name))
                {
                    cts.Add(item);
                }
            }
            list.RootFolder.UniqueContentTypeOrder = cts;
            list.RootFolder.Update();
        }
        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 void ModifyView(SPList list, IEnumerable<string> ViewFields, string query)
        {
            // Validation
            list.RequireNotNull("list");

            ViewFields.RequireNotNull("ViewFields");

            if (ViewFields.Count() == 0)
            {
                throw new ArgumentException("ViewFields");
            }

            query.RequireNotNullOrEmpty("query");

            SPView listView = (from SPView v in list.Views
                               where v.DefaultView == true
                               select v).First();
            listView.Query = query;

            listView.ViewFields.DeleteAll();
            foreach (string fieldName in ViewFields)
            {
                listView.ViewFields.Add(fieldName);
            }
            listView.Update();
        }
Пример #17
0
        /// <summary>
        /// Writes messages longer than 255 characters
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="list">The list.</param>
        private void WriteLongMessage(string message, SPList list)
        {
            message.RequireNotNullOrEmpty("message");
            list.RequireNotNull("list");
            if (message.Length <= 255)
            {
                WriteMessage(message, list);
            }

            else
            {
                for (int i = 0; i < message.Length; i += 255)
                {
                    int length = Math.Min(message.Length - i, 255);
                    WriteMessage(message.Substring(i, length), list);
                }
            }
        }