SharePoint

Populate DropDownList from List in SharePoint 2010 Visual Web Part

SPWeb web = SPControl.GetContextWeb(HttpContext.Current);
                SPList supervisorList = web.Lists["SupervisorList"];

                if(supervisorList !=null)
                {
                    ddlSupervisor.Items.Clear();
                    foreach (SPListItem items in supervisorList.Items)
                    {
                        string supervisorName = items["SupervisorName"].ToString();
                        ddlSupervisor.Items.Add(supervisorName);
                    }
                    ddlSupervisor.DataBind();
                }

Advertisements
Standard
SharePoint

Get Current LoggedIn User without Domain Name in SharePoint 2010

private string ReturnCurrentUserWithoutDomainName()
        {
            string currentUserWithDomainName = SPContext.Current.Web.CurrentUser.Name.ToString();
            if (currentUserWithDomainName.Contains("\\"))
            {
                string[] words = currentUserWithDomainName.Split('\\');
                return words[1];
            }
            else
            {
                return currentUserWithDomainName;
            }
        }

Standard
SharePoint

How to use CAML Query on List in SharePoint 2010

The Query schema of Collaborative Application Markup Language (CAML) is used in various ways within the context of Microsoft SharePoint Foundation to define queries against list data. For example, you can assign CAML query strings to the Query property of several classes in the server and client object models in order to return data.
In the below example , I am going to show you how to filter from SP List like SQL Server Select Query from a table . The below code is equivalent to SQL Query like ::: SELECT * FROM EmployeeTable WHERE EmployeeName=@employeeName

SPWeb web = SPControl.GetContextWeb(HttpContext.Current);
            SPList leaveStorage = web.Lists["LeaveStorage"];
               
            SPQuery query = new SPQuery();
            query.Query = string.Format(@"<Where>
                                                <Eq>
                                                     <FieldRef Name='EmployeeName'/>
                                                     <Value Type='Text'>{0}</Value>
                                                </Eq>
                                          </Where>", currentUser);

            SPListItemCollection listItems = leaveStorage.GetItems(query);

Standard
SharePoint

Get all Groups of the Site and Groups of current loggedIn User in SharePoint 2010 porgrammatically

SPWeb web = SPContext.Current.Web;

//Retreiving the current logged in user
SPUser currentUser = web.CurrentUser;

// Get all the Groups of the Current Site
SPGroupCollection allGroups = web.Groups;

//Retrieving all the groups in the site/web by specific user
SPGroupCollection userGroups = currentUser.Groups;

string groupName_Member = "FirstSiteColl Members";
//Check is Current user a member of specific Group
bool memberInGroup = web.IsCurrentUserMemberOfGroup(userGroups[groupName_Member].ID);

Standard
SharePoint

How to add an Item in SP List in SharePoint 2010

here web is the SPWeb object that is my Root Site on which am going to add an Item in the list named MyList

using (SPSite currentSite = new SPSite(siteUrl))
{
SPWeb web = currentSite.RootWeb;
SPListItemCollection listItemCollection = web.Lists["MyList"].Items;
SPListItem spListItem = listItemCollection.Add();
spListItem["Title"] = "Anything";
spListItem["CustomerName"] = "Shahjada";
spListItem["Address"] = "Dhaka";
spListItem.Update();
}

Standard
SharePoint

How to update Field’s value of an Existing SP List

 

SPSecurity.RunWithElevatedPrivileges(delegate() {
using (SPSite oSPsite = new SPSite(Your SP Site Collection URL))
{
foreach (SPWeb oSPWeb in oSPsite.AllWebs)
{
SPList lst = null;
oSPWeb.AllowUnsafeUpdates = true;
try
{
lst = oSPWeb.Lists[Your List Name];
}
catch (Exception ex)
{
continue;
}
try
{
oSPField = lst.Fields.GetField("FileType");
}
catch
{
blnColumnFileTypeExist = false;
}
if (blnColumnFileTypeExist)
{
SPListItemCollection items = lst.Items;
foreach (SPListItem item in items)
{
item["FileType"] = "HMZ4";
item.Update();
}
}
}
}
});

Standard