Tuesday, August 30, 2011

Create a SharePoint Lists in Object Model


 using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace LINQLists.Features.Feature1
{
    [Guid("1d80182d-25a1-4809-8104-bd915d224c76")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        private void FixupField(SPList splist, string fieldInternalName)
        {
            FixupField(splist.Fields.GetFieldByInternalName(fieldInternalName));
        }

        private void FixupField(SPField spfield)
        {
            spfield.ShowInDisplayForm = true;
            spfield.ShowInEditForm = true;
            spfield.ShowInEditForm = true;
            spfield.ShowInNewForm = true;
            spfield.ShowInVersionHistory = true;
            spfield.ShowInViewForms = true;

            SPView defaultView = spfield.ParentList.DefaultView;
            defaultView.ViewFields.Add(spfield);
            defaultView.Update();
            spfield.Update();
        }      

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPWeb spWeb = (SPWeb)properties.Feature.Parent)
            {
                Guid pListGuid = spWeb.Lists.Add("Projects", "Company Projects",

SPListTemplateType.GenericList);
                spWeb.Update();               
      
                SPList pList = spWeb.Lists[pListGuid];       
                pList.OnQuickLaunch = true;
                SPField pTitleIDField = pList.Fields["Title"];
                FixupField(pList, pList.Fields.Add("Description", SPFieldType.Text, false));
                FixupField(pList, pList.Fields.Add("Due Date", SPFieldType.DateTime, false));
                SPFieldDateTime dueDateField = (SPFieldDateTime)pList.Fields["Due Date"];
                dueDateField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
      
                dueDateField.Update();
                pList.Update();       
      
                Guid eListGuid = spWeb.Lists.Add("Employees", "Employees",

SPListTemplateType.GenericList);               
                spWeb.Update();

                SPList eList = spWeb.Lists[eListGuid];
                eList.OnQuickLaunch = true;
                SPField titleIDField = eList.Fields["Title"];
                titleIDField.Title = "Fullname";
                titleIDField.Update();
      
                FixupField(eList, eList.Fields.Add("JobTitle", SPFieldType.Text, false));
                FixupField(eList, eList.Fields.Add("Team", SPFieldType.Text, false));
                FixupField(eList, eList.Fields.Add("Contribution (in Milestones)",

SPFieldType.Number, false));
                string projectFieldInternalName = eList.Fields.AddLookup("Project", pListGuid,

false);
                SPFieldLookup projectField =

(SPFieldLookup)eList.Fields.GetFieldByInternalName(projectFieldInternalName);
                projectField.LookupField = pTitleIDField.InternalName;

                FixupField(projectField);
                eList.Update();       
      
                string employeeFieldInternalName = pList.Fields.AddLookup("Primary Contact",

eListGuid, false);
                SPFieldLookup managerField =

(SPFieldLookup)pList.Fields.GetFieldByInternalName(employeeFieldInternalName);
                managerField.LookupField = titleIDField.InternalName;
                FixupField(managerField);
                pList.Update();
            }
        }
        
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPWeb spWeb = (SPWeb)properties.Feature.Parent)
            {
                SPList empList = spWeb.Lists["Employees"];
                empList.Delete();
                spWeb.Update();
                SPList proLists = spWeb.Lists["Projects"];
                proLists.Delete();
                spWeb.Update();
            }
        }
        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}
        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //} 
        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string
upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}

No comments:

Post a Comment