Tuesday, December 22, 2009

Programmatically Deleting versions from a Sharepoint Document library

Some programming tasks are straight forward with SharePoint API. But, some times, you gotta be tricky.
There is a trick to delete the versions from a SharePoint document library.
each time a version is deleted from the document library, item index will be changed. So, the version item collection gets flushed for each and every delete. To deal with this item Index change, we need to itereate through all the folders and files and make sure to delete the first indexed file always(file.Versions[0].Delete()). This makes sure that the lowest indexed item will be deleted for each and every index flush.

SPSite site = new SPSite("http://servername/sites/sitecollname");
SPWeb web = site.OpenWeb();  
web.AllowUnsafeUpdates = true;
SPDocumentLibrary docLib=(SPDocumentLibrary)web.Lists["Documents"];
foreach (SPListItem folder in docLib.Folders)
     for (int i = 0; i < folder.Folder.Files.Count; i++)

SPFile file = folder.Folder.Files[i];
int counter = file.Versions.Count;

for (int j = 0; j < counter; j++)
if (file.Versions[0] != null)


web.AllowUnsafeUpdates = false;

Friday, December 18, 2009

Display name synchromization with the AD changes

Sharepoint user display name doesnot change with the AD changes.
Check the status of the sync:
STSADM -o sync -listolddatabases 0
check the timestamp that shows the sync. If not in sync follow the below.
  1. Go to SCA -> SSP-> User Profiles and My Sites -> User profiles and properties
  2. Start the full import to synchronize the chagnes in the AD or to remove the un wanted profiles. To update the Profile info with the new changes in the AD, just go for the incremental update.
  3. Click on the import source or import schedule link to get the Configure Profile Import page.
  4. Click on the "Refresh" to affact the changes.