Thursday, 7 January 2016

Updating a SharePoint LookUp column of List Item.

Updating a SharePoint List Item.


This post started after trying to solve an issue for a SharePoint Developer regarding Lookup columns for SharePoint. I started to test the same code in the Microsoft Virtual Labs and trying to solve the issue turned out to be a useful learning experience for me. Updating a list item seemed much simpler. I first created the "Sample1" Parent list containing the values
1. Approved
2. Pending
Then I created a second list "Sample2" list and added the custom Lookup column from the "Sample1" list.
Then, while testing in Powershell, the new columns are not appearing. Magically after sometime, the new columns are showing up. I believe this may be due to longer time for updated list to appear in Powershell. I realised my mistake.
The following script in Powershell does not show the expected new columns immediately.
(Get-SpWEb -identity "http://w15-sp").Lists["Sample2"].Items[1] |select Title,Status --WRONG
Get-SpWEb -identity "http://w15-sp").Lists["Sample2"].Items[1]["Status"]
The custom  fields are stored in the "XML" COLUMNS and cannot be viewed as you see in the Browser of a SharePoint site.
Below is a code to update lookup column in SharePoint site

Add-PSSnapin "Microsoft.SharePoint.Powershell"
$wpSite = Get-SPWeb "http://w15-sp"
$secondListItem = $wpSite.Lists["Sample2"].Items[1]
$secondListItem["Status"] = "1;#Approved"
$secondListItem.Update();
$secondListItem["Status"]



I tested the same updation using C#

using (SPSite wpSite = new SPSite("http://w15-sp"))
            {
                SPWeb wpWeb = wpSite.OpenWeb();
                //Get list "Sample1" with LookUp values 1:Approved;2:Rejected
                SPList lstSample1 = wpWeb.Lists["Sample1"];
                //Actual list to be updated
                SPList lstSample2 = wpWeb.Lists["Sample2"];
                SPListItem secondItem = lstSample2.Items[1];
                Console.WriteLine("Old values {0} {1}", secondItem["Title"], secondItem["Status"]);
                SPFieldLookupValue newValue = new SPFieldLookupValue(2, "Rejected");
                secondItem["Status"] = newValue;
                secondItem.Update();
                //After Update
                Console.WriteLine("New values {0} {1}", secondItem["Title"], secondItem["Status"]);
               
                Console.ReadLine();
            }





Tuesday, 5 January 2016

SharePoint Development Basics Part 1

The goal of this series is to create very basic projects for SharePoint in Visual Studio. Many times, we have starting trouble. If you are new to SharePoint, this tutorial can lay foundation for excellence in the existing SharePoint work you. You may already be developing or maintaining codes without understanding. After this series, hopefully, what seems complex is actually simple. The same code to retrieve a SharePoint object property would be done from simple Console to complex Solutions.

My favourite is C# Console Application and was actually first struggling to get the code running. On third day, magically the code ran after testing the SPSite against GUID instead of actual Url. In case you get errors like "The Web Application cannot be found..", check the following:
1. Run Visual Studio as Administrator
2. Set target platform to x64
3. .NET framework 4.5
4. Test the code against ID instead of string parameters which might have type error.

Generic steps for a SharePoint Development Project:
1.Open Visual Studio
2.Create New Project(x64)
3.Add SharePoint dll (from hive) [C:\ProgramFiles\Common Files\Microsoft shared\web server extentions\15\ISAPI]
4.Add using directive [Microsoft.SharePoint]
5.Write code
6.Debug

Below is the video illustrating the detailed steps for a simple Console Application reading the Site Collection properties: