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

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