Monday, December 23, 2013

Migrate SharePoint 2010 code (custom solutions) to SharePoint 2013

Introduction

Recently I was assigned to Migrate code which used to develop SharePoint 2010 web parts to SharePoint 2013 web parts. Here I will illustrate how I did it.

Solution

Step 01: Open the Visual Studio 2010 (SharePoint 2010) solution in Visual Studio 2012. 




Step 02: Change the Target framework to .Net Framework 4.5

Step 03: Remove all the SharePoint 2010 assemblies and add SharePoint 2013 assemblies. This is because SharePoint 2010 assemblies are located in 14 hive and in SharePoint 2013 the assemblies are located on 15 hive. The SharePoint dlls can be


  • Microsoft.Office.Server.dll
  • Microsoft.SharePoint.dll 
  • Microsoft.SharePoint.Security.dll 

There are others as well. Please refer MSDN for other common used SharePoint assemblies.

Step 04: Change Control templates/ web part location as below.

From
~/_CONTROLTEMPLATES/{Folder Name}
To
~/_CONTROLTEMPLATES/15/{Folder Name}

Step 05: Change the _layouts location files to /_layouts/15/ location. Usually images, CSS and JavaScript files are added to _layouts folder.

Images
From
/_layouts/images/
To
/_layouts/15/images/

CSS
From
/_layouts/{Folder Name}/css
To
/_layouts/15/{Folder Name}/css

JavaScript
From
/_layouts/{Folder Name}/scripts
To
/_layouts/15/{Folder Name}/scripts

Step 06: In all aspx pages you have to change the reference from SharePoint 2010 asssembly to SharePoint 2013 assemblies. To do this change the assembly version from Version=14.0.0.0 to Version=15.0.0.0

Example.

           


<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>



To

           

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


Conclusion
Hope this will work for you and please note that sometimes you will need to change the deployment path according to SharePoint 2013. As the above add 15 hive after the mapped folder. If you have set the master page as v4.master you should change to seattle.master

Tuesday, December 10, 2013

Create web properties in SharePoint using C#

Introduction

Most of the people use web.config to store values and use those values across multiple web parts. Sometimes editing web.config become mess and some teams don't want to do so. In a situation like this we can use web properties.

Solution

Here is the code and also you can download the .sln file from my drive by clicking below URL.
https://drive.google.com/file/d/0ByEnOE8DAdvhbVozSURYZU9naWM/edit?usp=sharing

     
namespace WebProperty.WebProperty
{
    [ToolboxItemAttribute(false)]
    public class WebProperty : WebPart
    {
        protected override void CreateChildControls()
        {
            Label label = new Label();


            SetWebProperty("NoOfLabels", "10");//Set the property. If the property exists it set and if not create and sets. 

            int maximumNo = Convert.ToInt32(GetWebProperty("NoOfLabels"));//Get the web property value
            for (int i = 0; i < maximumNo; i++)
            {
                label.Text = string.Format("{0}{1}", label.Text, i);
            }
            this.Controls.Add(label);
        }

        void SetWebProperty(string Name, string Value)
        {
            using (SPSite spSite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb spWeb = spSite.OpenWeb(SPContext.Current.Web.ID))
                {
                    GetWebProperty(Name); //ensure the property exists
                    spWeb.AllowUnsafeUpdates = true;
                    spWeb.AllProperties[Name] = Value;//Set the value for the property 
                    spWeb.Update();
                    spWeb.AllowUnsafeUpdates = false;
                }
            }
        }
        static string GetWebProperty(string Name)
        {
            using (SPSite spSite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = spSite.OpenWeb(SPContext.Current.Web.ID))
                {
                    //Check whether property exists
                    if (web.AllProperties[Name] == null)
                    {
                        web.AllowUnsafeUpdates = true;
                        web.AllProperties.Add(Name, "");//Creates a new web property
                        web.Update();
                        web.AllowUnsafeUpdates = false;
                        return string.Empty;
                    }
                    else
                    {
                        return web.AllProperties[Name].ToString();
                    }
                }
            }
        }
    }
} 

Conclusion

You may store values in web property instead of web.config whenever you need.

Friday, December 6, 2013

How to create a SharePoint webpart with custom webpart property/properties

Introduction

Recently one of colleague want to create a webpart for SharePoint using C# as programming language. One of his requirements is to that “user wants to pass values through custom webpart property.” So I created a sample for him.

Solution

Here is the code and also you can download the .sln file from my drive by clicking below URL.
https://drive.google.com/file/d/0ByEnOE8DAdvhLTFtd1oxWS1GNUU/edit?usp=sharing

     
namespace CustomProperty.CustomWebPartProperty
{
    [ToolboxItemAttribute(false)]
    public class CustomWebPartProperty : WebPart
    {
        private string _name = "Suhail";//The default value to display
        [WebBrowsable(true),
        WebDisplayName("Label Text"),//WebPart property display name
        WebDescription("Label Text to appear in Label"),//Property discription
        Category("Custom"),//Custom webpart property catogory
        Personalizable(PersonalizationScope.Shared)]
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }

        }
        protected override void CreateChildControls()
        {
            Label name = new Label();
            name.Text = Name;
            this.Controls.Add(name);

        }
    }
}
 

Conclusion

You can see your custom webpart property at the end of the webpart setting.



When you need values which should be able customize don’t save in web.config file; You may use custom webpart properties.

Sunday, December 1, 2013

Adding Left Navigation for SharePoint Publishing site programmatically

Problem Background

I was working on creating a webtemplate based in SharePoint 2013 based on publishing site. There I had an requirement to create Left Navigation programmatic.

Solution Background

Here I have used "Structural Navigation" as the navigation type.

Solution

Below is the method to achieve it.

           
using (SPSite spSite = new SPSite("http://cd-sjamaldeen:23855/sites/002/"))
{
    using (SPWeb spWeb = spSite.OpenWeb())
    {

        //to remove pages from quick launch navigation
        PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(spWeb);
        // Current navigation 
        publishingWeb.Navigation.CurrentIncludePages = false;
        publishingWeb.Navigation.CurrentIncludeSubSites = false;
        publishingWeb.Navigation.GlobalIncludePages = false;
        publishingWeb.Navigation.GlobalIncludeSubSites = false;


        var webNavSettings = new WebNavigationSettings(spWeb);

        // Set the current navigation to Structural Navigation type
        webNavSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;

        // Set the global navigation to Structural Navigation type
        webNavSettings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;

        webNavSettings.Update();
      

        //add the settings page to the quick launch
        SPNavigationNodeCollection nodeColl = spWeb.Navigation.QuickLaunch;

        //Delete all the navigation nodes from QuickLaunch 
        for (int i = spWeb.Navigation.QuickLaunch.Count - 1; i > -1; i--)
        {
                        
            spWeb.Navigation.QuickLaunch[i].Delete();
        }


        publishingWeb.Update();

        SPNavigationNode googleNode = new SPNavigationNode("Google", "http://google.com");
        nodeColl.AddAsFirst(googleNode);


        SPNavigationNode yahooNode = new SPNavigationNode("Yahoo", "http://yahoo.com");
        nodeColl.AddAsLast(yahooNode);

        spSite.AllowUnsafeUpdates = true;
        spWeb.AllowUnsafeUpdates = true;
        spWeb.Update();
        spWeb.AllowUnsafeUpdates = false;
        spSite.AllowUnsafeUpdates = false;

    }
}
 
 

Conclusion

Hope your publishing site now will have the Navigation.
If you face any problems go to site setting ->  Navigation (Under Look and Feel section) see weather all your navigation setting exists.