Tuesday, January 12, 2016

Creating Event Recevier in SharePoint using Visual Studio 2012


In this article we can see how to create a new Event Receiver for SharePoint 2013 with Visual Studio 2012.
Follow the bellow steps to create a new Event Receiver
1. Create a new Empty SharePoint 2013 Project.





2. Select Solution as a Farm.

















3. Right click the project and select Add new Item and select create event receiver and click ok.



4. In the event receiver settings dialog select the “List Item Events” as a type of event receiver and “custom List” in the item source drop down. Select “An Item was Added” from Handle the following event and click finish.




5. In the event receiver class of list , add the below mentioned code. This is just a simple example to change the title to current date.


   public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            SPListItem _currentItem = properties.ListItem;
            _currentItem["Title"] = DateTime.Now.ToString();
            _currentItem.Update();
        }
6. Now build and deploy the project to the SharePoint

7.
In SharePoint Site click Site Actions => Site Settings => Galleries =>Solution and verify that your solution is now deployed to the site.

8. Add new Item list the title will update with current date.


Monday, January 11, 2016

Creating Webpart in SharePoint using Visual Studio 2012

In this article we can see how to create a new Visual Web Part for SharePoint 2013 with Visual Studio 2012.
Follow the bellow steps to create a new Visual Web Part
1. Create a new Empty SharePoint 2013 Project.


 2. Select Solution as farm solution.
 
3. Add new item as visual Webpart
4. Now solution will open in visual studio.Add three labels in .ascx file
<asp:Label ID="lblTitle" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblFirstName" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblLastName" runat="server" Text="Label"></asp:Label>
5. Write below code in page load in  ascx.cs file to access SharePoint list data in Web part.

 string strUrl = "http://spdev-3:1001";
            using (SPSite oSite = new SPSite(strUrl))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    SPList list = oWeb.Lists["TestList"];
                    SPQuery query = new SPQuery();
                    query.Query = "<Where><Eq> <FieldRef Name='Title' /><Value Type='Text'>PERM</Value></Eq></Where>";
                    query.RowLimit = 1;
                    SPListItemCollection spItemCollection = list.GetItems(query);
                    SPListItem spItem=spItemCollection[0];
                    lblTitle.Text = spItem["Title"]!=null?spItem["Title"].ToString():string.Empty;
                    lblFirstName.Text = spItem["FirstName"] != null ? spItem["FirstName"].ToString() : string.Empty;
                    lblLastName.Text = spItem["LastName"] != null ? spItem["LastName"].ToString() : string.Empty;
                
                }
            }

6. Right click on solution and deploy web part.

7. Add web part in SharePoint Page.After adding web part on page the data will show on a page.


8. Now you able to access SharePoint List data in Web part.

Tuesday, November 3, 2015

SharePoint GetFieldValueAsHtml or GetFormattedValue with DateTime fields Show One Day Earlier

Question:

What am I doing incorrectly here?  I simply want to get the date value from an SPFieldDateTime, which I thought was working until I looked into it closer:

1) I input 10/01/2008 into the field
2) in the code, it consistently reads 09/30/08
(tested with mulitple dates, it always removes a day from the inputted value)

the kicker is, however, that the field contains the correct date!!!
see below, the object is the same throughout:

?field.GetFieldValue (listItem(field.Id))
"10/1/2008" {String}
String: "10/1/2008"
?field.GetFieldValueAsText (listItem(field.Id))
"9/30/2008"
?field.GetFieldValueForEdit (listItem(field.Id))
"9/30/2008"
?field.GetFieldValueAsHtml (listItem(field.Id))
"9/30/2008"
?listitem(field.Id).ToString
"10/1/2008 12:00:00 AM"

Answer:

Watch out when using GetFieldValueAsHtml or GetFormattedValue with SharePoint DateTime fields! They both expect the value to be in UTC! However, SPListItem[FieldName] returns its value in Local Time zone and marks it as Unspecified.

Basically, what you have to do prior to calling one of the two methods above is make sure your time is in UTC:

DateTime localTime = (DateTime)listItem[“SomeDateTime”];
DateTime universalTime = this.Web.RegionalSettings.TimeZone.LocalTimeToUTC(localTime);
string correctlyConverted = listItem[“SomeDateTime”].GetFieldValueAsHtml(universalTime);