Friday, 4 January 2013

Read list values & store temporarily in Table structure to perform operation


DataTable demoTable;

/// Function to define table structure

demoTable = CreateTableStructure();
foreach (SearchResult result in searcher.FindAll())
{
                UserDisplayName = GetProperty(result, EmpDisplayName);
Designation = GetProperty(result, EmpDesignation);
Department = GetProperty(result, EmpDepartment);
EMail = GetProperty(result, EmpEMail);

/// Create rows in the table & insert values in every column.

DataRow row1 = demoTable.NewRow();
row1["userdisplayname"] = UserDisplayName;
row1["designation"] = Designation;
row1["department"] = Department;
row1["email"] = EMail;
demoTable.Rows.Add(row1);
}
/// Function to read values from data table one by one & perform further operations

  UpdateList(demoTable);

/// Function to read value of a specified property from Active Directory
private string GetProperty(SearchResult searchResult, string PropertyName)
{
if (searchResult.Properties.Contains(PropertyName))
{
return searchResult.Properties[PropertyName][0].ToString();
}
else
{
return string.Empty;
}
}

/// Function to create table structure

public DataTable CreateTableStructure ()
{
            DataTable demotab = new DataTable();

            DataColumn displayname = new DataColumn("userdisplayname");
            DataColumn designation = new DataColumn("designation");
            DataColumn department = new DataColumn("department");
            DataColumn email = new DataColumn("email");

            displayname.DataType = System.Type.GetType("System.String");
            designation.DataType = System.Type.GetType("System.String");
            department.DataType = System.Type.GetType("System.String");
            email.DataType = System.Type.GetType("System.String");

            demotab.Columns.Add(displayname);
            demotab.Columns.Add(designation);
            demotab.Columns.Add(department);
            demotab.Columns.Add(email);
           
            return demotab;
}

/// Function to perform Operations on List by reading each row of table

public void UpdateList (DataTable demoTable)
{
string UserDisplayName;
string Designation;
string Department;
string EMail;

if (demoTable.Rows.Count > 0)
{
for (int i = 0; i <= demoTable.Rows.Count - 1; i++)
{
UserDisplayName = demoTable.Rows[i]["userdisplayname"].ToString();
Designation = demoTable.Rows[i]["designation"].ToString();
Department = demoTable.Rows[i]["department"].ToString();
EMail = demoTable.Rows[i]["email"].ToString();
                   
//Perform List Update Operation
                                       
}
}
}

Remove Faulty Webpart from SharePoint Page


There was a custom web part deployed in SharePoint environment. For some reasons, web part encountered some issue and made the pages crashed!. Ideally, the developer should have handled the error in web part to avoid page crash! but it didn't happen!

Alright, to fix the issue, go into the Maintenance mode by just appending "?contents=1" to the end of URL. The Web Part Maintenance Page is opened where the list of all the web parts used on the page is seen. Select the particular web part and delete that particular web part and we are back to business.


jQuery to hide SharePoint Fields in Add/Edit/Display Form in SharePoint 2010


Place the Below script in the CEWP within that page 

<script src="http://sharepoint-site/_layouts/1033/jquery-1.4.4.minf.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('td.ms-formlabel:contains("Field-Internal-Name")').parent().hide();
});
</script>

Empty RecycleBin in SharePoint 2010


There are total 2 Recycle bins in every site collection in SharePoint. First Recycle Bin can be called as end user recycle bin which is available in every site & sub-site of a particular site collection. This Recycle Bin is accessible through Site Actions-> View All Site Content. Whenever the user deletes the document, it is stored in this Recycle Bin.

Now, when the document from first Recycle Bin is deleted it gets stored in the Second Recycle Bin which can also be called as Admin Recycle Bin. This is available only at site collection level. This Recycle Bin is accessible through Site Actions-> Site Settings-> Site Collection Administration.

$WebApp=get-spwebapplication "http://sharepoint-site-name"

    foreach ($SPSite in $webApp.Sites)
    {
         #get the collection of webs
      foreach($SPWeb in $SPSite.AllWebs)
         {
             #Empty the 1st Stage Recycle bin items PERMENANTLY which is available in All Site Content
             #$SPWeb.RecycleBin.DeleteAll();
             
             #Send the 1st Stage Recycle bin items to 2nd Stage
             $SPWeb.RecycleBin.MoveAllToSecondStage();

              write-host "End-User Recycle Bin Items Deleted for:"
              write-host $SPWeb.title ":" $SPWeb.URL "`n"
         }
             #Empty SharePoint site collection recycle bin (2nd Stage Recycle bin) or Admin Recycle bin which is available in the Site Settings page of root site collection
             $SPSite.RecycleBin.DeleteAll();

  write-host "Administrator Recycle bin Items Deleted for:" $SPSite.RootWeb.title "`n"
    }

Powershell script to Delete Document By Name in SharePoint 2010


Below is the Powershell script to delete document by Name from Document Library

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite("http:// sharepoint-site-url ")
$web = $site.openweb()
$list=$web.Lists["Document-Library-Name "]
$listItems = $list.Items
$listItemsTotal = $listItems.Count
Write-Host $listItemsTotal
for ($x=$listItemsTotal-1;$x -ge 0; $x--)
{
  if($listItems[$x].name.Contains("file")) # file refers to the    name of the document
  {
    Write-Host("DELETED: " + $listItems[$x].name)
    $listItems[$x].Delete()
  }
}

Powershell script to Delete Document By ID in SharePoint 2010


Below is the Powershell script to delete document by ID from Document Library

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite("http://sharepoint-site-url")
$web = $site.openweb()
$list=$web.Lists["Document-Library-Name"]
$listItems = $list.Items
$listItemsTotal = $listItems.Count
Write-Host $listItemsTotal
for ($x=$listItemsTotal-1;$x -ge 0; $x--)
  {
    if($listItems[$x].id -eq 777) # 777 is the ID of the document
    {
      Write-Host("DELETED: " + $listItems[$x].name)
      $listItems[$x].Delete()
    }
  }

SharePoint 2010 Content Type IDs - Reference


SharePoint 2010 Content Type IDs - Reference
Content Type
ID
 System
0x
 Item
0×01
 Document
0×0101
 Event
0×0102
 Issue
0×0103
 Announcement
0×0104
 Link
0×0105
 Contact
0×0106
 Message
0×0107
 Task
0×0108
 Workflow History
0×0109
 Post
0×0110
 Comment
0×0111
 East Asia Contact
0×0116
 Folder
0×0120