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:
STEP 2:Click Save List as template

Enter filename and template name

Go to Site Actions->Site Settings->Galleries->List Template Click on the List needed and save in any location as .stp .

Steps to Create a Sharepoint Project File ->New ->Project->EmptySharepointProject

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

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(SPFeatureReceiverPropertiesproperties)
        {
            SPWeb spWeb = (SPWeb)properties.Feature.Parent;
            string path = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\UserProfileList\";
            Hashtable CustomList = newHashtable();
            DirectoryInfo di = newDirectoryInfo(path);
            if (di.Exists)
            {
               FileInfo[] rgFiles = di.GetFiles("*.stp");
               foreach (FileInfofi in rgFiles)
               {
                   CustomList.Add(fi.Name, path);
               }
               if (CustomList.Count > 0)
               {
                   foreach (DictionaryEntryitem in CustomList)
                   {
                       UploadTemplate(spWeb.Site, spWeb, item.Value.ToString() + item.Key.ToString(), item.Key.ToString(), "description");
                   }
               }
            }
        }
        public static void UploadTemplate(SPSitespSite, SPWeb spWeb, string CustomListStpLocalPath, stringCustomLisName, string description)
        {
            try
            {
               SPSecurity.RunWithElevatedPrivileges(delegate()
               {
                   using (spSite = newSPSite(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 (Exceptionexp)
            {
               Console.WriteLine(exp.GetType().Name);
            }
        }
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.
        {
            // SPSite spSite = (SPSite)properties.Feature.Parent;
            SPWeb spWeb = (SPWeb)properties.Feature.Parent;
            SPListTemplateCollection listTemplates = spWeb.Site.GetCustomListTemplates(spWeb);
            foreach (SPListTemplatetemplate in listTemplates)
            {
               EnsureList(spWeb, template.Name, template, false);
            }
            // SetLookupColumns(spWeb);//Uncomment this line when look up exist
        }
        private voidSetLookupColumns(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 (DictionaryEntryitem 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(SPWebsite, string listName, SPListTemplatetemplate, bool onQuickLaunch)
        {
            SPList list = null;
            Guid listID = Guid.Empty;
            if (site != null)
            {
               foreach (SPListitem 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 newException("In EnsureSiteDataList SPWeb is null");
            }
            return list;
        }
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.
 
 

 
No comments:
Post a Comment