May 04, 2012

Steps to move Custom List to a production server

STEP 1:

Let us Start with Back up of the custom List before creating sharepoint Project

Goto the Site ->View all Site content ->Click on the list need to take backup ->List Settings
 STEP 2:
Click Save List as template
STEP 3:

Enter filename and template name

STEP 4:

Go to Site Actions->Site Settings->Galleries->List Template

Click on the List needed and  save in any location as .stp .


STEP 5:
Steps to Create a Sharepoint Project 

File ->New ->Project->EmptySharepointProject


STEP 6:

Now place all the Back up List(.stp) in Layouts->UserProfileList  folder




STEP 7:

Double  click the Features and enter the Description for the Feature 1
STEP 8:
Right click the Feature1.Feature and Click add Event Receiver then double  click the Features1 .EventReceiver and copy the below  code and paste in Features1.EventReceiver
 
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb spWeb = (SPWeb)properties.Feature.Parent;
            string path = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\UserProfileList\";
            Hashtable CustomList = new Hashtable();
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists)
            {
                FileInfo[] rgFiles = di.GetFiles("*.stp");
                foreach (FileInfo fi in rgFiles)
                {
                    CustomList.Add(fi.Name, path);
                }
                if (CustomList.Count > 0)
                {
                    foreach (DictionaryEntry item in CustomList)
                    {
                        UploadTemplate(spWeb.Site, spWeb, item.Value.ToString() + item.Key.ToString(), item.Key.ToString(), "description");
                    }
                }
            }

        }
        public static void UploadTemplate(SPSite spSite, SPWeb spWeb, string CustomListStpLocalPath, string CustomLisName, string description)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (spSite = new SPSite(SPContext.Current.Site.Url))
                    {
                        spSite.AllowUnsafeUpdates = true;
                        using (spWeb = spSite.OpenWeb())
                        {
                            spWeb.AllowUnsafeUpdates = true;
                            //get the template gallery
                            SPDocumentLibrary list = (SPDocumentLibrary)spWeb.Lists["List Template Gallery"];

                            //get the folder under which the site tempaltes reside
                            SPFolder myFolder = list.RootFolder;
                            //get the byte data of the site tempalte from the disk
                            byte[] data = File.ReadAllBytes(CustomListStpLocalPath);
                            //add the file to the folder                       
                            SPFile file = myFolder.Files.Add(CustomLisName, data, true);
                            //update the changes
                            file.Update();
                            spWeb.Dispose();
 
                        }
                    }
                    spSite.AllowUnsafeUpdates = false; spWeb.AllowUnsafeUpdates = false;
                });
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.GetType().Name);
            }
        }

STEP 9:
Double  click the Features and enter the Description for the Feature 2

STEP 10:

Right click the Features2.Feature and Click add Event Receiver the double  click the Features2 .EventReceiver and copy the below  code and paste in Features2.EventReceiver

If you want to set Look up for the List use the function setLookupcolumns shown below.



     public override void FeatureActivated(SPFeatureReceiverProperties properties)

        {

            // SPSite spSite = (SPSite)properties.Feature.Parent;

            SPWeb spWeb = (SPWeb)properties.Feature.Parent;

            SPListTemplateCollection listTemplates = spWeb.Site.GetCustomListTemplates(spWeb);

            foreach (SPListTemplate template in listTemplates)

            {

                EnsureList(spWeb, template.Name, template, false);

            }

            // SetLookupColumns(spWeb);//Uncomment this line when look up exist



        }



        private void SetLookupColumns(SPWeb SPwebsite)

        {



            using (SPwebsite)

            {

                //Lookup List 1



                Hashtable hshtable = new Hashtable();

                //ParentList,ID                   //ChildList,ID    

                hshtable.Add("PatientProfile1,PID", "PatientProfile,ID");

                hshtable.Add("FrequencyBk,FrequencyID", "FrequencyList,ID");

                hshtable.Add("FrequencyBk,PatientID", "PatientProfile,ID");

                hshtable.Add("FrequencyBk,TherapistID", "CareGiverProfile,ID");

                hshtable.Add("FrequencyList,PatientID", "PatientProfile,ID");

                hshtable.Add("FrequencyList,TherapistID", "CareGiverProfile,ID");

                hshtable.Add("VisitScheduleList,PatientID", "PatientProfile,ID");

                hshtable.Add("VisitScheduleList,TherapistID", "CareGiverProfile,ID");

                hshtable.Add("VisitScheduleList,FrequencyID", "FrequencyList,ID");

                hshtable.Add("CareGiverFileList,CareGiverID", "CareGiverProfile,ID");

                hshtable.Add("AssignTherapistMail,PatientID", "PatientProfile,ID");

                hshtable.Add("OTEvaluationCon,OTID", "OTEvalustion,ID");

                hshtable.Add("OTEvaluationCon1,OTID", "OTEvalustion,ID");

                hshtable.Add("SpeechTherapy2,SPID", "SpeechTherapy1,ID");

                hshtable.Add("PTRangeofMotion,PTEvaluation1_ID", "PTEvaluation1,ID");

                hshtable.Add("PTEvaluation2,PTEvaluation1_ID", "PTEvaluation1,ID");

                hshtable.Add("PTRangeofMotion,PTReAssessment_ID", "PTReassessment,ID");

                hshtable.Add("VisitScheduleList,PatientEvent", "PatientEventList,Title");



                if (hshtable.Count > 0)

                {

                    foreach (DictionaryEntry item in hshtable)

                    {

                        //Parent List

                        SPList lookupList = SPwebsite.Lists.TryGetList(item.Value.ToString().Split(',').GetValue(0).ToString());

                        //Child List

                        SPList relatedList = SPwebsite.Lists.TryGetList(item.Key.ToString().Split(',').GetValue(0).ToString());



                        if (lookupList != null && relatedList != null)

                        {

                            relatedList.Fields.Delete(item.Key.ToString().Split(',').GetValue(1).ToString());

                            string strPrimaryCol = relatedList.Fields.AddLookup(item.Key.ToString().Split(',').GetValue(1).ToString(), lookupList.ID, true);

                            SPFieldLookup primaryCol = (SPFieldLookup)relatedList.Fields.GetFieldByInternalName(strPrimaryCol);



                            primaryCol.LookupField = lookupList.Fields[item.Value.ToString().Split(',').GetValue(1).ToString()].InternalName;

                            primaryCol.Indexed = true;

                            primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Cascade;

                            primaryCol.Update();

                        }

                    }



                }

            }



        }



        public static SPList EnsureList(SPWeb site, string listName, SPListTemplate template, bool onQuickLaunch)

        {



            SPList list = null;

            Guid listID = Guid.Empty;

            if (site != null)

            {

                foreach (SPList item in site.Lists)

                {

                    if (item.Title.ToLower() == listName.ToLower())

                    {

                        list = item;

                        listID = item.ID;

                        break;

                    }

                }



                if (list == null)

                {

                    listID = site.Lists.Add(listName, "", template);

                    list = site.Lists[listID];

                    list.OnQuickLaunch = onQuickLaunch;

                    list.Update();

                }

            }

            else

            {

                throw new Exception("In EnsureSiteDataList SPWeb is null");

            }

            return list;

        }







STEP 11:

Finally deploy the project to the site and activate the Features.

Site Actions->Site Settings ->Manage Site Features ->Activate UserprofileList Feature1 and  UserprofileList Feature

Note: Feature1 must be activated first.

STEP 12:
After activating Features check whether List exist in  Site actions->View all site content ->Lists

No comments:

Post a Comment