Pages

Wednesday, April 16, 2014

The property 'opacity' is not compatible with Internet Explorer 8.0

Introduction

I had to set opacity value for a text using cascading style (CSS). But when I type opacity it showed “The property 'opacity' is not compatible with Internet Explorer 8.0” in Visual Studio. See the screen shot below. 



Solution

To set the opacity value you have to set both opacity and a filter value; so that it will work for all browsers. 

   
p {
    opacity: 0.8;//0.0 is fully transparent and 1.0 is fully opaque 
    filter: alpha(opacity=80);//Here 0 is transparent and 100 is fully opaque 
}
 

Conclusion

Reference: 
Programming with HTML5 with JavaScript and CSS3 – A Microsoft Learning Product
 

Wednesday, April 9, 2014

Redirect to an external website from SharePoint App

Introduction

I’m assigned to a project where I’m to develop SharePoint hosted app using JSOM.

Problem Background

There I wanted to redirect to external web page. Eg.http://google.com. I used 

  • location.href='http://google.com'; 
  • window.location.replace("http://google.com");
  • document.location.href = url;
which we use normally to redirect to another website. But they didn’t work for me. This is because there was a post back on my page and I still want it.

Solution


I set a Timeout and it worked for me; use the below code to redirect.


   
  setTimeout(function () { document.location.href = url }, 20);

Conclusion


Hope this will help you. :) 
 

Friday, March 28, 2014

How to get the correct audit occurred time from SPAuditEntry

Introduction

I was assigned to a project where I had to retrieve audit data programmatically using C#. I used SPAuditQuery and SPAuditEntry in order to achieve this.

Problem Background

When I retrieve the audit occurred time by spAuditEntry.Occurred; it gives a different value from the actual time.

Solution Background

SharePoint save the audit data in content database in table dbo.AuditData. To see auditing data; open your content database and go to the table dbo.AuditData and see the column “Occurred” there you can see the UTC Date time of the actual audit time. 





I learnt from various blog posts that SharePoint usually store datetime in UTC DateTime format in the content database; and when it want to display the date time in SharePoint site the value stored in database is converted to webapplication’s time and then displays the time according to the time zone the person is in.

Solution

To get the web application time you have to call RegionalSettings.TimeZone.UTCToLocalTime() method.


   
spWeb.RegionalSettings.TimeZone.UTCToLocalTime(spAuditEntry.Occurred)
//spAuditEntry.Occurred is the time stored in content database which is a in DateTime format

Conclusion

So when you retrieve any data from content database programatically  which contains datetime;  please use the above method as you need. I have already blogged on a same kind of issue here. This was to retrieve the LastItemModifiedDate as Local Time.

Wednesday, March 12, 2014

Set application page heading dynamically in SharePoint

Introduction

I was had to set the heading of the application page dynamically from the code behind (server side).


Solution

Your application page will look something like this.

 Applicationpage

What you can do is simply replace "My Application Page" with a label and you can replace the text from the back end.

  1. <%-- This is Heading of the page --%>
  2. <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
  3. <asp:Label runat="server" ID="labelPageTitle"></asp:Label>
  4. </asp:Content>

Now from the code behind you can access the label by its id and set the text as you need.

  1. labelPageTitle.Text = "From code behind";


Conclusion

Your page will look something like this.

apppage

Wednesday, February 19, 2014

Display notifications in SharePoint from code behind

Introduction

Sometimes we may need to display notifications in SharePoint; which appears on the right side of the page below the ribbon.

Problem Background

We may need to call the notification from the code behind which I will show how to achieve it.

Solution Background

We have to call SP.UI.Notify class to add and remove notification. This class contain two methods


  • addNotification – Add a notification to the page and returns the id of that notification.
  • removeNotification – Removes the notification from the page. 

Solution
 

   
private void ShowNotification()
        {
            var scriptTemplate = @"
ExecuteOrDelayUntilScriptLoaded(function(){{
    ExecuteOrDelayUntilScriptLoaded(function(){{
            var notificationId = SP.UI.Notify.addNotification(""{0}"", false);
//false here will remove the notification in 5 secs
//true will not remove until we remove it by calling removeNotification   
        }},
    'core.js'
    )}},
'sp.js'
);";
            //You can give the string in Hello World!!
            var script = string.Format(scriptTemplate, "Hello World!!");
            //Here Any String as the key of the client script to register
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "Any String", script, true);
        }

Use removeNotification to remove the notification explicitly.

   
private void ShowNotification()
        {
            var scriptTemplate = @"
    ExecuteOrDelayUntilScriptLoaded(function(){{
        ExecuteOrDelayUntilScriptLoaded(function(){{
            var notificationId = SP.UI.Notify.addNotification(""{0}"", true);
            removeNotification(notificationId);//remove the notification. 
        }},
    'core.js'
    )}},
'sp.js'
);";
      
            var script = string.Format(scriptTemplate, "Hello World!!");
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "Any String", script, true);
        }

Conclusion

Please go through below urls for more information on this. 

 

Thursday, February 13, 2014

Retrieve Author of SPListItem as SPUser in SharePoint; programmatically (C#)

Introduction
Updated on 15th July 2014

I was assigned in a project where I had to retrieve the author of the SPListItem as SPUser programmatically.

Solutions

In these solutions I’m returning the login name. But you can get the SPUser.

Solution 01

  1. public string GetListItemAuthorNameSPBuiltInFieldId(SPListItem spListItem)
  2.       {
  3.           string loginName = string.Empty;
  4.           SPWeb spWeb = SPContext.Current.Web;
  5.  
  6.           var fullUserName = spListItem[SPBuiltInFieldId.Author] as string;
  7.           var userName = fullUserName.Split('#')[1];//This is only to get the user name. My fullUserName was 1;#Setup Admin.
  8.           SPUser spUser = spWeb.EnsureUser(userName);//You can get SPUser from here
  9.           loginName = spUser.LoginName;
  10.           return loginName;
  11.  
  12.       }
It is recommended to SPBuiltInFieldId in order to retrieve build in fields.

Solution 02

  1. public string GetListItemAuthorName(SPListItem spListItem)
  2.   {
  3.       string loginName = string.Empty;
  4.       var spFieldUser = spListItem.Fields.GetFieldByInternalName("Author") as SPFieldUser;
  5.  
  6.       if (spFieldUser != null && spListItem["Author"] != null)
  7.       {
  8.           var fieldValue = spFieldUser.GetFieldValue(spListItem["Author"].ToString()) as SPFieldUserValue;
  9.           if (fieldValue != null)
  10.           {
  11.               var spUser = fieldValue.User;//Get the SPUser
  12.               loginName = spUser.LoginName;//Get the login name from SPUser
  13.           }
  14.       }
  15.       return loginName;
  16.   }


Conclusion

I will update how to retrieve values from SPField in future days.

Friday, February 7, 2014

Query all the documents by its file type from a Site and its all sub sites using SPSiteDataQuery

Introduction

I had to develop a web part where I had to query and display all the document items which the file extensions are “docx” under a particular site collection including its sub sites. I used SPSiteDataQuery class in order to achieve.

Solution

 
using (SPSite spSite = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb spWeb = spSite.OpenWeb())
                {
                    SPSiteDataQuery spSiteDataQuery = new SPSiteDataQuery();

                    spSiteDataQuery.Query =
                    @"<Where>
      <Eq>
         <FieldRef Name='DocIcon' />
         <Value Type='Computed'>docx</Value>
      </Eq>
   </Where>";
                    spSiteDataQuery.ViewFields = "<FieldRef Name='FileLeafRef' /><FieldRef Name='Editor' /><FieldRef Name='ContentType' />";
                    spSiteDataQuery.Lists = "<Lists ServerTemplate='101'/>"; //Set the list template by providing its template type id

                    spSiteDataQuery.Webs = "<Webs Scope='Recursive'/>";//Recursive: Current site and any subsite
                    //Scope='SiteCollection': all webs in the current site collection

                    //Get the results into a datatable
                    DataTable resultsTable = spWeb.GetSiteData(spSiteDataQuery);

                    
                    //Bind the results in to a asp grid view
                    GridView spGridView = new GridView();
                    spGridView.AutoGenerateColumns = true;
                    spGridView.DataSource = resultsTable;
                    spGridView.DataBind();

                    Controls.Add(spGridView);
                }
            }

 
Conclusion 
The results will look something as below. 



I have posted the my sln file here. Please download in case if you need
https://drive.google.com/file/d/0ByEnOE8DAdvhSTlnMDBuWDRuWTA/edit?usp=sharing

Reference: http://msdn.microsoft.com/en-us/library/ms409088.aspx