April 24, 2014

SharePoint how to check user, CurrentUser is in perticuler site group

You wants to check if CurrentUser or particular User / SPUser is present in specific site's permission group or not,
Usually site have three groups Visitors, Members and Owners. If you have create another group and wants to check if particular user is present is that group for some validation so you can refer below code sample,

* Assuming you have basic knowledge of SharePoint coding.



Code:
private bool UserPresentInGroup(string groupName)
        {
            bool isAdmin = false;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                { // User may not have permission to see site group or Group members....
                    using (SPSite oSite = new SPSite(strWebUrl))
                    {
                        using (SPWeb oWeb = oSite.OpenWeb())
                        {
                            SPGroup oUserGroup = oWeb.Groups[groupName];
                            foreach (SPUser item in oUserGroup.Users)
                            {
                                if (item.LoginName == currentUser.LoginName)
                                {
                                    isAdmin = true;
                                }
                            }
                        }
                    }
                });
            }
            catch (Exception exp)
            {}// Catch exception

If you wants to check user from Sites default Visitor, Member or Owner Group you can directly get group by below property
  • SPWeb.AssociatedVisitorGroup
  • SPWeb.AssociatedMemberGroup
  • SPWeb.AssociatedOwnerGroup
instead of  oUserGroup.


--
Thanks,
Praveen Pandit

March 16, 2014

SharePoint read csv or xml file from wsp / solution of _layouts file.





SharePoint how to read any csv or xml file in webpart using C# code.

Usually we doesn’t required this scenario, but in some cases you want to keep any config data or aby xml data apart from typical SPList you have to create a Config or default setting file that cabn be access by your C# code,



Finally you want to get file that you have mapped in _layouts so using StreamReader / Stream or File.Open(); it needs file’s absolute path and if you are in SharePoint wsp so that makes you to follow approach like below,

In file path string pass below way


SharePoint – C# get xml file from _layouts
Server.MapPath(“/_layouts/mySolutionFolder/myXml.xml”);
Read external file in SharePoint C#







Thanks,

Praveen Pandit




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