Friday, 1 April 2016

Changing Back Color of SP List Column in View


I have implemented in one of my previous project using below script.
Where we want to keep the background color of status field based on the value of Status and DueDate Field.


(function () {
    // Create object that have the context information about the field that we want to change it's output render
    var statusFiledContext = {};
    statusFiledContext.Templates = {};
    statusFiledContext.Templates.Fields = {
        // Apply the new rendering for status field on List View
"Status": { "View": statusFiledTemplate },
"DueDate": { "View": dueDateFiledTemplate }
    };
               

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(statusFiledContext);
               
})();



// This function provides the rendering logic for list view
function statusFiledTemplate(ctx) {
    var status = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
    switch (status) {
                case "Not Started":
return "<span style='background-color :rgb(186, 20, 26); color :WhiteSmoke; -moz-border-radius:5px 5px 5px 5px;border-radius:5px 5px 5px 5px; padding-right: 5px;padding-left: 5px' >" + status + "</span>";
                                break;
                 case "In Progress":
return "<span style='background-color :rgb(252, 209, 22);-moz-border-radius:5px 5px 5px 5px;border-radius:5px 5px 5px 5px; padding-right: 5px;padding-left: 5px'>" + status + "</span>";
                                break;
                case "N/A":
return "<span style='background-color :rgb(210, 210, 210);-moz-border-radius:5px 5px 5px 5px;border-radius:5px 5px 5px 5px; padding-right: 5px;padding-left: 5px'>" + status + "</span>";
                case "Completed":
                                 return "<span style='background-color :rgb(85, 212, 85);-moz-border-radius:5px 5px 5px 5px;border-radius:5px 5px 5px 5px; padding-right: 5px;padding-left: 5px'>" + status + "</span>";
    }
}



function dueDateFiledTemplate(ctx) {
                 var duedate = ctx.CurrentItem.DueDate;
                var currentDate = new Date();
                var day = currentDate.getDate();
                var month = currentDate.getMonth() + 1;
                var year = currentDate.getFullYear();
                var today_date = month+"/"+day+"/"+year;
                var firstValue = duedate.split('/');
                var secondValue = today_date.split('/');

                var dueDate = new Date();
                dueDate.setFullYear(firstValue[2],(firstValue[0] - 1 ),firstValue[1]);

                var todayDate = new Date();
                todayDate.setFullYear(secondValue[2],(secondValue[0] - 1 ),secondValue[1]);
               
                 var status = ctx.CurrentItem.Status;
                if (dueDate < todayDate && (status == "Not Started" || status == "In Progress"))
                {
                                return "<span style='background-color :rgb(186, 20, 26); color :WhiteSmoke ; -moz-border-radius:5px 5px 5px 5px;border-radius:5px 5px 5px 5px; padding-right: 5px;padding-left: 5px'>" + duedate + "</span>";
                }
                else
                {
                                return duedate;
                }
}

Monday, 21 March 2016

Load Vs LoadQuery

Load vs Loadquery

The LoadQuery method works in much the same way as the Load method, but it allows the client to process the queries more efficiently. The main difference is that the Load method populates the client object or client object collection that is passed into it. The LoadQuery method instead returns an entirely new collection of client objects. To get a clarification of these differences, examine the following examples for .NET managed code.
Retrieving the Number of Lists via the Load Method
class Program
{
static void Main(string[] args)
{
ClientContext ctx = new ClientContext(“http://server”);
ctx.Load(ctx.Web.Lists);
ctx.ExecuteQuery();
Console.WriteLine(“” + ctx.Web.Lists.Count);
Console.ReadLine(“Press RETURN…”);
}
}
In the above example Console.WriteLine(“” + ctx.Web.Lists.Count); will write the count of lists in the web since the ctx.Web.Lists gets loaded with the lists in the context.
Now lets see the other example, which uses the LoadQuery method, which is also initialized with the ctx.Web.Lists object. However, after the query is executed, the lists are returned as a completely new client object collection. If you try to directly access the property ctx.Web.Lists.Count, a CollectionNotInitialized exception is raised. This implies that, unlike in the above code, the original ctx.Web.Lists property has not been populated. Instead, a new allLists collection has been created and populated.
Retrieving the Number of Lists via the LoadQuery Method
class Program
{
static void Main(string[] args)
{
ClientContext ctx = new ClientContext(“http://server”);
IEnumerable allLists = ctx.LoadQuery(ctx.Web.Lists);
ctx.ExecuteQuery();
Console.WriteLine(“” + allLists.Count());
Console.ReadLine(“Press RETURN…”);
}
}
The advantage of LoadQuery over the Load method is its flexibility, especially when working with more than one query. It gives you better control over memory consumption, and query processing is more efficient. Consider garbage collection: the Load method populates objects that reside within the ClientContext instance. Those objects can only be cleaned up when the ClientContext instance is destroyed. Conversely, objects created by LoadQuery are separate from the ClientContext instance and can be destroyed much more readily, for example by setting them to NULL.

Step by step guide on upgrading a SharePoint 2010 site to a SharePoint 2013

  1. Add and deploy any required O14 custom solutions in your SharePoint 2013 farm, notice that all O14 solutions being deployed under 14 hive but eventually it’s recommended to migrate the custom code to O15.
  2. Backup and restore the SharePoint 2010 content database to your SQL Server hosting SharePoint 2013 farm.
  3. Run Test-SPContentDatabase cmdlet to identify missing components along with potential errors and related warnings. Check the upgrade log and deploy any missing components and re run the cmdlet to verify.
  4. Attach the content database to the desired web application using Mount-SPContentDatabase cmdlet.
  5. After successfully mounting the content database to web application, the site should be accessible in 14 mode.
  6. To upgrade the site simply click on “Start now” link on the toolbar, you can also go to SiteUpgrade page from Site Setting page as well.
  7. Click on “TRY A DEMO UPGRADE” link to verify the site collection upgrade (This step can be performed by Site Collection Administrators, farm admins can run
    Request-SPUpgradeEvaluationSiteCollection cmdlet as well).
  8. Provision a temporary site collection to validate the site post-upgrade.
  9. An email should be sent out to Site Collection admin when the temporary site is provisioned.
  10. After validating the temporary site we upgrade the site by going to SiteUpgrade.aspx page and clicking on “Upgrade the Site Collection” button (Farm admins can run Upgrade-SPSite cmdlet as well).
  11. During the upgrade The SiteUpgrade.aspx page shows the progress and provides a link to an upgrade log for troubleshooting purposes.
  12. Your site collection should now be accessible in 15 mode along with all new capabilities enabled in the farm and for the hosting web application such as Social and Office Web Apps.
Note: If there are any reference you manually added to CSS, JS, Images or any other files in your custom solutions which are in Layouts, you need to change the path of these references to point it to 15 hive layout.
e.g.
14 Hive – _/layouts/myPlugin.js
15 Hive – _/layouts/15/myPlugin.js



Sunday, 20 March 2016

Send Email Using SMTP Client

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("ToEmailAddress");
message.Subject = "The subject";
message.From = new System.Net.Mail.MailAddress("FromEmailAddress");
message.Body = "Email Body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("Address to your SMPT server");
smtp.Send(message);