Showing posts with label Deployment. Show all posts
Showing posts with label Deployment. Show all posts

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

April 03, 2012

How to deploy wsp file using Visual Studio 2010 for SharePoint 2010

In SharePoint 2010 it is easy to create a .wsp file using visual studio 2010, which was previously very difficult in MOSS 2007. To deploy any solution package in any local environment in Visual Studio 2010 simply right click on the project and deploy. But to deploy the same thing in the production server we will need to deploy through .wsp file.


Steps to generate the wsp file:
 Right click on the project/solution in Visual Studio 2010 then choose package. If you are building the project/solution in debug mode then you will get the .wsp file inside the Bin\Debug folder and if you are building the project in release mode then you will get the .wsp file inside the Bin\Release folder. This wsp file is needed to deploy in the production environment.

This is the best way to create the wsp file in visual studio 2010.

There are different ways you can deploy the .wsp file

Using browser:

Go to the Central Administration -> Site Actions ->Site Settings -> Galleries –>click on Solutions. After this the below page will open as shown in the figure.

Click on Upload solution and then browse to the wsp file as shown below in 

the figure.

Once you will click Ok then activation window will appear like below figure.


Then you can use the solution.

Using PowerShell:

To add a solution using PowerShell:
Open the SharePoint 2010 Management Shell. You can get to this by going to Start > All Programs > Microsoft SharePoint 2010 Products > SharePoint 2010 Management Shell.

Add-SPSolution {path of wsp file}

To deploy the solution using power shell
Install-SPSolution –Identity {path of wsp file}.wsp –WebApplication http:// -GACDeployment

To update solution
Update-SPSolution –Identity {path of wsp file}.wsp –LiteralPath {local path of wsp file}wsp –GACDeployment

To uninstall solution
Uninstall-SPSolution –Identity {path of wsp file}.wsp –WebApplication http://

Remove solution
Remove-SPSolution –Identity {path of wsp file}.wsp

Using Stsadm:

Open (Dos prompt) then type cd c:"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin"

Add the solution using the following command
stsadm -o addsolution -filename {path of wsp file}

Deploy the solution
stsadm -o deploysolution -name {path of wsp file} -url {URL}

Retract solution:
stsadm.exe -o retractsolution -name {path of wsp file}.wsp –URL

Delete Solution
stsadm.exe -o deletesolution -name {path of wsp file}.wsp