Search This Blog

Thursday, 26 July 2012

Get SPUser Object from a SharePoint list item people or group field value.

Hi All,

Refer the below code to get the SPUser object from a People of Group column. To call the function you have pass the listitem and the fieldname (display name).

public SPUser GetSPUserObject(SPListItem spListItem, String fieldName)
            SPUser spUser = null;
                if (fieldName != string.Empty)
                    SPFieldUser field = spListItem.Fields[fieldName] as SPFieldUser;
                    if (field != null && spListItem[fieldName] != null)
                        SPFieldUserValue fieldValue = field.GetFieldValue(spListItem[fieldName].ToString()) as SPFieldUserValue;
                        if (fieldValue != null)
                            spUser = fieldValue.User;
            catch (Exception ex)
                throw ex;
            return spUser;

Reference :

Tuesday, 24 July 2012

Get the domain name from SharePoint 2010 Url

Scenario : I have to get the domain name alone from the SharePoint 2010 Url,

Ex: http://trig:1986  ------------> Url
      here the domain name in trig. Now i need to get this in my code behind.

I found the solution for this. Refer the below code snippet

            Uri urlnew = new Uri("http://triad102:1990");
            string split = urlnew.Scheme + Uri.SchemeDelimiter + urlnew.Host + ":" + urlnew.Port;
            Console.WriteLine("Full Url :: http://triad102:1190");
            Console.WriteLine("Scheme :: " + urlnew.Scheme);
            Console.WriteLine("SchemeDelimiter :: " + Uri.SchemeDelimiter);
            Console.WriteLine("Host :: " + urlnew.Host);
            Console.WriteLine("Port :: " + urlnew.Port);

Result :

Monday, 23 July 2012

Error occurred in deployment step ‘Activate Features’: Object reference not set to an instance of an object.

Do you get this error while deploying solution with a Feature Event Receiver through Visual Studio 2010. Well, I did while deploying a custom timer job solution which has a feature receiver to activate and deactivate the timer job feature.

Somewhere I got to know this error occurs if we have used SPContext class in our feature activate event receiver. But, when I checked my event receiver I did not use this class at all.
Then, I attached the solution to OWSTimer.exe and started debugging to trace the error. I then found that I have accessed the SPSite of the feature as its parent as shown below.

And the scope of the feature was “Web” as shown below

The Parent property of a feature represents the object that represents the scope of the feature. So, in my case I have assigned the Parent property of the feature (Which is  SPWeb object) to the SPSite object instead and thus the mismatch caused the error while deploying.