Saturday, December 17, 2011

Set LookUp field for Custom Lists

Step1:After Creating all Lists, add another feature in same project,
Step2:add an EventReceiver in that
Step3:Paste FollowingCodes in that:


  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);
          
        }
        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", "PatientEventList1,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;
        }
Step4:deploy the solution

No comments:

Post a Comment

Creating Provider hosted app (sharepoint online) with local hosted IIS

The Pre-requires are as follows. 1. Office 365 Subscription 2. Visual Studio 2015 (Professional/Community/Enterprise Edition) With t...