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");
}


 

Thursday, December 13, 2012

How to call\launch an exe and pass arguments to it using powershell

Scenario: How to launch an exe and pass arguments to it using powershell?

Solution:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"
$webApp = read-host "Enter the URL of the SharePoint Web app"
$contentDBs = Get-SPContentDatabase -WebApplication $webApp
foreach($cdb in $contentDBs)
{
& 'C:\test\test.exe' -webappurl $webApp -Dbname $cdb.Name
}

Wednesday, December 12, 2012

Sharepoint 2010: Stop Re-direction to Mysite person.aspx page

Scenario: On a multi-auth enabled SAML webapp, If you click under welcome sign-in control "About Me" it will re-direct user to My-Site person.aspx page. Its all good as long as, that logged in user has a mysite. But, in scenario were the users doesn't have a mysite created, It will prompt for user credentials, which might be annoying. But this is OOB SharePoint behavior.How can you stop this re-direction?

Solution: Actually, as soon as a user profile gets created for that logged in user, it will start re-directing user to person.aspx page. But, instead clients want to get re-directed to "userdisp.aspx" page.

Well,This can be done by deleting the user profile so that it will always gets re-directed to "userdisp.aspx". But, the profile gets re-created again. And in a scenario were there are thousands of user this solution is not viable.

Actually, behind the scenes, there is a OOB user control called "MySiteRedirection.ascx" that is tied to a delegate control "DelctlProfileRedirection" on userdisp.aspx page that does this re-direction. That control is tied to this userdip.aspx page via OOB feature named "MySite".

So the custom solution to stop the mysite re-direction will be to create your own custom user control, with no code (just empty default usercontrol) and place it under "_controltemplates" . Then create a feature.xml and elements.xml file to load your custom user control inside the "ProfileRedirection" delegate control, with a sequence number less than "100". If the sequence number is not set to less than 100, your custom control will not be loaded.

For more info about understanding OOB delegate controls refer to:-
http://www.helpmeonsharepoint.com/2012/04/sharepoint-delegate-controls.html

And to know how to customize and load your own controls refer to:-
http://panvega.wordpress.com/2008/10/12/sharepoint-delegate-controls/
http://sharepointmagazine.net/articles/custom-page-security-using-sharepoint-delegate-controls
http://blogs.msdn.com/b/sowmyancs/archive/2009/02/01/how-to-create-a-custom-delegate-control-with-vsewss-1-3.aspx