Thursday, January 3, 2013

Delete\Remove custom Site Colums using SharePoint object model

Scenario: Delete\Remove Site Columns using SharePoint Object model code

Solution:  Add a feature receiver and in FeatureUninstalling write below code
SPWeb myWeb = properties.Feature.Parent as SPWeb;
myWeb.Fields["{GUID of Site column to delete}"].Delete();

SharePoint2013 Enable Licensing and office webapps editing for all SAML custom claims provider users

Scenario: How to enable licensing\ Office webapps edit for all SAML custom claims provider users

Solution: In SharePoint2013 management shell execute below commands
1) $allCustomProviderUsers = New-SPClaimsPrincipal -EncodedClaim "c:0!.s|trusted%3aCustomClaimsProvidername"

Get-SPWebApplication | select Url | %{New-SPUserLicenseMapping -Claim $allCustomProviderUsers –License "Enterprise" -WebApplication $_.Url | Add-SPUserLicenseMapping}
Enable-SPUserLicensing

2)
$allCustomProvideraccount = New-SPClaimsPrincipal -EncodedClaim "c:0!.s|trusted%3aCustomClaimsProvidername"

Get-SPWebApplication | select Url | %{ New-SPUserLicenseMapping -Claim $allCustomProvideraccount

-License "OfficeWebAppsEdit" -WebApplication $_.Url | Add-SPUserLicenseMapping}

 Replace "CustomClaimsProvidername" with your custom claims provider name in your environment.

 

Friday, December 28, 2012

SharePoint Object model code to delete items from large lists

Scenario: You have thousands of items in a SharePoint list and you want to delete them, in a way that query should not take longer to execute.

Solution:
1. Construct a CAML query for each item in a list you want to delete
2.  You can then pass on the aggregated CAML query for all the items to OOB ProcessBatchData() function as below:-
private static StringBuilder BuildBatchDeleteCommand(SPList list)
{
StringBuilder sbDelete = new StringBuilder();
 
                sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

                string command = "<Method><SetList Scope=\"Request\">" + list.ID +

                    "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
  
foreach (SPListItem item in list.Items)
{
 sbDelete.Append(string.Format(command, item.ID.ToString())); 

 sbDelete.Append("</Batch>");
 return sbDelete;
}


  string strBatchDelete = BuildBatchDeleteCommand(list).ToString();
string strProcessBatch = site.RootWeb.ProcessBatchData(strBatchDelete);

This will place the items in a recycle bin. So to delete items from the recycle bin use
web.RecycleBin.Delete()

 Refer to below form post to do it using powershell:-

http://sharepoint.stackexchange.com/questions/26542/deleting-all-the-items-from-a-large-list-in-sharepoint

Wednesday, December 19, 2012

SharePoint: Finding assembly public key token value

Scenario: Sometimes when you are working with building custom solutions in SharePoint, before you deploy the solution binaries to GAC. You need to know the public key token.

Solution: To find public key token follow below steps:
1. Open visual studio command prompt
2. Navigate to "bin\debug" directory of the current solution
3. Type :-
    Sn  -T  Yourdllname.dll

SharePoint2010: Modify BreadCrumb links on a page

Scenario: How to modify/change breadcrumb navigation links on a specific page

Solution: In a Content editor webpart add below code
<script type=text/javascript>
document.getElementById('PlaceHolderTitleBreadCrumb').innerHTML =
"<a href='/pages/default.aspx'>HomePage</a> > 
<a href='/Departments'>Departments</a> >    
<a href='/Departments/IT'>IT</a> > IT News";
</script>
 
    

SharePoint2010: Hide BreadCrumb on a specific page

Scenario: How to hide a breadcrumb on a specific page

Solution: Place a Content Editor webpart on a page. and paste below code
<script type=text/javascript>
document.getElementById('PlaceHolderTitleBreadCrumb').innerHTML = "";
</script>

SharePoint2010:Delete\Remove masterpages, css files, themes on feature deactivation

Scenario: How to delete Masterpages, css files and themes from the respective galleries on feature deactivation

Solution: Write following code on feature deactivation event
SPFile myTheme = System.Web.GetFile("_catalogs/theme/MyCustom.thmx");
if (myTheme.Exists)
{  
  myTheme.Delete("_catalogs/theme/MyCustom.thmx");
}