February 20, 2014

SharePoint How to get SPUser from list item C#



How to get SPUser from list item, SharePoint list item get user, how to get created, modified, Author or Editor from sharepoint list, how to get LoginName or Email of User from list,

Well there is another way to get Web. EnsureUser(“User”); it returns SPUser object as well it adds user in site directly if do not already that web user, another way to get it write a function that return SPUser object,   

*Assuming that you are aware how to get SPWeb and List then List item,

SPUser oUserSubmittedBy = GetSPUser(oItem, “fieldDisplay Name”);

Here is the function body.



Code:
protected SPUser GetSPUser(SPListItem item, string user)

        {

            SPUser oUser = null;

            SPFieldUserValue userValue = new SPFieldUserValue(item.Web, Convert.ToString(item[user]));

            if (userValue != null)

            {

                oUser = userValue.User;

            }

            return oUser;

        }


And once you get object you can get Name, email and login name

oUserSubmittedBy.LoginName
oUserSubmittedBy.Name
oUserSubmittedBy.Email…. etc..



--
Regards,
Praveen Pandit

January 09, 2014

Best way to get Query string from url in SharePoint / Asp .Net / webApps



The easiest and well know way is to Request.QueryString[“name”]; but many times to get null exception we should check query string and return in appropriate format. Below I have created a private int variable and protected property of it. You can have a look on below code.


protected int ItemID
        {
            get
            {
                if (Request.QueryString["itemId"] != null && Request.QueryString["itemId"].Length > 0)
                {
                    return Convert.ToInt32(Request.QueryString["itemId"]);
                }
                else return 0;
            }
        }



You can Share your findings or issue or Discussion below.

--
Regards,
Praveen Pandit


December 29, 2013

SHarePoint get List of Specific (Task, GenericList, DocumentLibrary, Announcements) Object Model C#



 SharePoint saves everything in format of list below are few type of Lists those we use frequently.
  • GenericList
  • DocumentLibrary
  • Survey
  • Announcements
  • Contacts
  • Tasks
Complete List Type template can be found here

Now some time it happens your clients come up with some requirement and you have to get all list of specific Type of SPList. its not big deal ;)

Suppose I have 20 list few are Task type few Announcement Type and few are generic list, Now I wants only those list whose Template Type is Announcement.


C# Code:
using (SPWeb oWeb = SPContext.Current.Site.OpenWeb("\RelaviteUrl",false))
            {
                foreach(SPList oList in oWeb.Lists)
                {
                    if (SPListTemplateType.Announcements == oList.BaseTemplate)
                    {
                        //(oList.Title);
                    }                
                }                
            }



Please share any issue or findings on this. 


--
Regards,
Praveen Pandit