Tuesday, 20 January 2015

Adding Lists Programmatically in SharePoint

Developers often look for creating Custom Lists in SharePoint sites Programmatically.
Here is a sample code for creation of SharePoint Lists Programmatically
public void createSPList(string strListName)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
    //Open your site
    SPSite site = new SPSite(Constants.strSiteURL);
    SPWeb web = site.OpenWeb();
    web.AllowUnsafeUpdates = true;
    SPListCollection lists = web.Lists;
    // create new Generic list
    lists.Add(strListName, "My Description", SPListTemplateType.GenericList);
    //Add Columns to SP List
    SPList newSPList = web.Lists[strListName];
    // create new column "User Name" and Field Type “Text”
    newSPList.Fields.Add("User Name", SPFieldType.Text, true);
    //creating lookup type column to this New List
    //Here I am getting information from the "Title" column from “Employee Roles” List
    SPList List2 = web.Lists["Employee Roles"];
    newSPList.Fields.AddLookup("Lookup Column", List2.ID, false);
    SPFieldLookup spLookup = (SPFieldLookup) newSPList.Fields["Lookup Column"];
    spLookup.LookupField = List2.Fields["Title"].InternalName;
    spLookup.Update();
    // Adding newly added column to default view
    SPView view = newSPList.DefaultView;
    view.ViewFields.Add("User Name");
    view.ViewFields.Add("Lookup Column");
    view.Update();
    newSPList.Update();
    web.AllowUnsafeUpdates = false;
    site.Dispose();
});
}



SharePoint List generation depends on the List Template type that we are specifying during list creation. one can create lists of type Custom Lists(Generic Lists), Document Library, Announcements, Picture Library, Tasks, Survey, Discussion Board, Contacts, Links, etc.

You can specify list template type in the below code highlighted

 lists.Add(strListName, "My Description", SPListTemplateType.GenericList);

Rating Scale in SharePoint Survey List Programmatically

In SharePoint Survey List, Rating scale is very easy to implement, set properties and use this in SharePoint. I found an easy way to implement this rating scale in SharePoint using coding in Feature activation.

When you are Creating a survey list programmatically, add this field to the SharePoint survey list.


Code:
//Adding Question to Survey List Programmatically with rating scale
strQuestionTitle = "Lecturer Skills";
StringCollection strQuestions = new StringCollection();
strQuestions.Add("Approachability");
strQuestions.Add("Availability");
strQuestions.Add("Share Knowledge");
strQuestions.Add("Motivate");
strQuestions.Add("Provide guidance");
objSurvey.Fields.Add(strQuestionTitle, SPFieldType.GridChoice, true, false, strQuestions);
//Updating Rating scale field properties
SPFieldRatingScale srs = objSurvey.Fields[strQuestionTitle] as SPFieldRatingScale;
srs.GridTextRangeLow = "Low";
srs.GridTextRangeHigh = "High";
srs.GridTextRangeAverage = "Avg";
srs.GridStartNumber = 1;
srs.GridEndNumber = 5;
srs.GridNAOptionText = "NA";
srs.Update();
//Updating survey list
objSurvey.Update();
 Hope this article is helpful and here ends your searching.

PowerShell Commands : Add, Install, Update, UnInstall, Delete WSP

Working with PowerShell Commands is very easy:

Copy your WSP file to an appropriate location e.g. C:\<foldername>

Launch PowerShell on the SharePoint server Start -> All Programs -> Microsoft SharePoint 2013 Products -> SharePoint 2013 Management Shell


Adding SharePoint WSP package using PowerShell command
To add a WSP package, you must use the following PowerShell command.

Add-SPSolution “<WSP package path>”
 

e.g.: Add-SPSolution “C:\testfile.wsp”
more info on : MSDN


Installing SharePoint WSP package using PowerShell command
To Install a WSP package, you must use the following PowerShell command.


Install-SPSolution -Identity “<wsp package name>” -WebApplication “<url of web application>” -GACDeployment
 

e.g.: Install-SPSolution -Identity “testfile.wsp” -WebApplication “http://localhost:11111” -GACDeployment
more info on : MSDN


Uninstalling SharePoint WSP package using PowerShell command
To uninstall a WSP package, you must use the following PowerShell command.

Uninstall-SPSolution -Identity “<wsp package name>” -WebApplication “<url of web application>”
 

e.g.: Uninstall-SPSolution -Identity “testfile.wsp” -WebApplication localhost:11111”
more info on : MSDN


Removing SharePoint WSP package using PowerShell command
To remove a WSP package, you must use the following PowerShell command.

Remove-SPSolution -Identity “<wsp package name>”
 
e.g.: Remove-SPSolution -Identity “testfile.wsp”

more info on : MSDN
 




Updating SharePoint WSP package using PowerShell command

To update a WSP package, you must use the following PowerShell command.

Update-SPSolution -Identity “<wsp package name>” -LiteralPath “<physical path of the wsp package>” -GacDeployment
 
e.g.: Update-SPSolution -Identity “testfile.wsp” -LiteralPath “C:\testfile.wsp” -GacDeployment

more info on : MSDN

Hope this article helped your search. Thank You.

Create User Friendly Time in SharePoint Programmatically using C#.NET.

        
/// <summary>
        /// Method used to Get the Datetime in User Friendly Date Format
        /// </summary>
        public static string GetUserFriendlyTime(DateTime dt, DateTime dtNowDate)
        {
            TimeSpan span = dtNowDate - dt;
            if (span.Days > 365)
            {
                int years = (span.Days / 365);
                if (span.Days % 365 != 0)
                    years += 1;
                return String.Format("about {0} {1} ago",
                years, years == 1 ? "year" : "years");
            }
            if (span.Days > 30)
            {
                int months = (span.Days / 30);
                if (span.Days % 31 != 0)
                    months += 1;
                return String.Format("about {0} {1} ago",
                months, months == 1 ? "month" : "months");
            }
            if (span.Days > 0)
                return String.Format("about {0} {1} ago",
                span.Days, span.Days == 1 ? "day" : "days");
            if (span.Hours > 0)
                return String.Format("about {0} {1} ago",
                span.Hours, span.Hours == 1 ? "hour" : "hours");
            if (span.Minutes > 0)
                return String.Format("about {0} {1} ago",
                span.Minutes, span.Minutes == 1 ? "minute" : "minutes");
            if (span.Seconds > 5)
                return String.Format("about {0} seconds ago", span.Seconds);
            if (span.Seconds <= 5)
                return "just now";
            return string.Empty;
        }