Pages

Showing posts with label SP Object Model. Show all posts
Showing posts with label SP Object Model. Show all posts

Friday, May 17, 2013

Load Sharepoint List Items in the drop down list (Web part page) via SP Object Model


Load Sharepoint List Items in the drop down list (Web part page) via SP Object Model

 

 

private void populateAvailiability()

        {

 

            try

            {

 

                ddlAvailability.Items.Clear();

                string listName = "HR_Comp_Availability";

                SPSite site = SPContext.Current.Site;

                SPWeb web = SPContext.Current.Web;

                Guid webGuid = web.ID;

                Guid siteGuid = site.ID;

 

                //new code for spsite

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    using (SPSite currentSite = new SPSite(siteGuid))

                    {

                        currentSite.AllowUnsafeUpdates = true;

                        currentSite.CatchAccessDeniedException = false;

                        using (SPWeb itemWeb = currentSite.OpenWeb(webGuid))

                        {

                            SPList list = itemWeb.Lists[listName];

                            SPView view = list.DefaultView;

                            SPQuery query = new SPQuery();

                            query.ViewFields = "";

                            //query.Query = " ";

                            SPListItemCollection items = list.GetItems(query);

                            DataTable Availability2 = items.GetDataTable();

 

 

                            if (Availability2.Rows.Count > 0)

                            {

                                ddlAvailability.DataValueField = "Title";

                                ddlAvailability.DataTextField = "Title";

                                ddlAvailability.DataSource = Availability2;

                                ddlAvailability.DataBind();

                                //ddlAvailability.Items.Insert(0, new ListItem("select", "0"));

                            }

 

                            else

                            {

                                ddlAvailability.Items.Clear();

                            }

                        }

                    }

                });

 

            }

            catch (Exception ex)

            {

                this.lblError.Text += ex.Message + " -- " + ex.StackTrace;

            }

 

 

        }

Check User available in that Sharepoint Group or Not using SP Object Model


If the User available in that SP Group or Not via SP Object Model



private void GetSecurityGroup()

{

// Write your code here.

string SecurityGroupflag = "";

using (SPSite siteCollection = SPContext.Current.Site)

//using (SPSite siteCollection = new SPSite(properties.WebUrl))

{

using (SPWeb site = siteCollection.OpenWeb())

{

string groupName = "GWPSecurity";

//Get the current logged in user

SPUser currentUser = site.CurrentUser;

//Get all the user groups in the site/web

SPGroupCollection userGroups = currentUser.Groups;

//Loops through the groups and check if the user is part of given group or not.

foreach (SPGroup group in userGroups)

{

//Checking the group

if (group.Name.Contains(groupName))

{

SecurityGroupflag = "true";

break;

}

else

{

SecurityGroupflag = "false";

}

}

}

}

XPathNavigator datasource;

datasource = this.MainDataSource.CreateNavigator();

datasource.SelectSingleNode("/my:myFields/my:GWP_Security_Check_Group", NamespaceManager).SetValue(SecurityGroupflag);

}

Get Sharepoint Group User's Name and User Email id via SP Object Model


Get SP Group User's Name and User Email id via SP Object Model:

 

private void GetSecurity()

{

string GetSecurity = string.Empty;

string GetSecurityEmail = string.Empty;

using (SPSite site = new SPSite(SPContext.Current.Web.Url))

{

using (SPWeb web = site.OpenWeb())

{

SPGroup gp = web.Groups["GWPSecurity"];

if (gp != null)

{

foreach (SPUser usr in gp.Users)

{

//if (usr.Name != "System Account")

//{

if (GetSecurity == "")

GetSecurity += usr.Name;

//else

// GetSecurity += ", " + usr.Name;

if (GetSecurityEmail == "")

GetSecurityEmail += usr.Email;

else

GetSecurityEmail += ";" + usr.Email;

//}

}

}

}

}

XPathNavigator datasource;

datasource = this.MainDataSource.CreateNavigator();

datasource.SelectSingleNode("/my:myFields/my:NewSecurityName", NamespaceManager).SetValue(GetSecurity);

datasource.SelectSingleNode("/my:myFields/my:SecurityofficerEmail", NamespaceManager).SetValue(GetSecurityEmail);

}