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
Uncategorized

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
Uncategorized

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
SharePoint

How to Copy or move a list between sites in different site collections SharePoint 2010

Step 1: Create a list template
– Navigate to the site containing the list for which you want to create a list template.
– In the ribbon, click the List tab, and then in the Settings section, click List Settings.
– In the Permissions and Management column, click Save list as template.
Step 2: Download the list template as a file (.stp) from the list gallery of the source site
– Navigate to the top level site of the site collection for the source site that contains the list you want to copy or move.
– On the Site Actions menu Site Actions Menu, click Site Settings.
– In the Galleries section, click List templates.
– Download the desired List Template .
Step 3: Upload the list template file (.stp) to the list template gallery of the destination site
– Go to List Template Gallery from Site Settings .
– Upload the desired .stp file .

Done , your List template is now available in different Site Collection . So you can now create the List using this List Template.

Standard
SharePoint

How to add new Field in Existing SharePoint List

Here , I will show how we can add new Field in our existing SP List of Existing system .
In My case , Existing List Name is ::: MyList
Let , Our SP Site Collection has many sites and I dont know the actual number of Sites , But I need to Add Field in every Site , So we can do like below , which will add in all Sites inside of a Site Collection .
and strSiteURL is The URL of my Site Collection .

string strListName = "MyList";
SPField oSPField = null;
bool blnColumnFileTypeExist = true;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite oSPsite = new SPSite(strSiteURL))
{
foreach (SPWeb oSPWeb in oSPsite.AllWebs)
{
SPList lst = null;
oSPWeb.AllowUnsafeUpdates = true;
try
{
lst = oSPWeb.Lists[strListName];
}
catch(Exception ex)
{
continue;
}
try
{
oSPField = lst.Fields.GetField("FileType");
}
catch
{
blnColumnFileTypeExist = false;
}
if (!blnColumnFileTypeExist)
{
SPFieldText fldFileType = (SPFieldText)lst.Fields.CreateNewField(SPFieldType.Text.ToString(), "FileType");
fldFileType.ReadOnlyField = false;
lst.Fields.Add(fldFileType);
lst.Update();
}
}
}
});

Standard
SharePoint

How to enable Customize InfoPath Button in Ribbon in SharePoint 2010 SP List

Many new developers get problem with this issue all over the world .
Coz By default , the service is not activated . you need to activate this by yourself .

1. Go to Site Settings
2. Go to Site collection features .
3. Activate “SharePoint Server Enterprise Site Collection features” , then you create a new custom list , u will get the Customize Form By InfoPath Button in the Ribbon

Standard
Uncategorized

What is Site collection in SharePoint?

SharePoint site collection is a logical grouping of multiple SharePoint site or hierarchical site structure. For e.g. Sites of various teams or departments of an organization can be grouped logically in one site collection. A site collection consists of a top-level site and one or more sites below it. Each top-level site and any sites below it in the site structure are based on a site template and can have other unique settings and content. Hence, SharePoint site collection is a hierarchical set of sites that can be managed together. Sites within a site collection have common features, such as shared permissions, galleries for templates, content types, and Web Parts, and they often share a common navigation. A sub site can inherit permissions and navigation structure from its parent site or these can be specified and managed independently. Creation of sub sites can be delegated to users of a site collection, but a service administrator must perform creation of site collections

Standard
Uncategorized

Calling MS SQL SP (with param) from C#

By like below , we can call SP from C# . Here I am gonna show how a SP with Param can be called , which is little tricky rather than simple SP .

My SP name is ::: spU_ChangeProjectStatus Click Here to view the SP .

The SP has a param named @tblProjects_ID , and I am filling that param with projectId variable .

public static bool ChangeProjectStatus(int projectId)
        {
            try
            {
                using (SqlConnection cn = new SqlConnection(DBConnector.GetConnectionStringFromServer()))
                {
                    SqlCommand cmd = new SqlCommand("spU_ChangeProjectStatus", cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@tblProjects_ID", SqlDbType.Int).Value = projectId;
                    cn.Open();
                  
                    cmd.ExecuteNonQuery();
                    return  true;
                }
            }
            catch (Exception ex)
            {
                // Logger will be added here  Later
                return false;
            }
        }
Standard