Saturday, 27 October 2012

How to Programmatically Approve documents in Sharepoint

Hi,

A requirement was posted to me where a developer wanted to approve the documents programmatically. I shared the code snippet below and it the logic worked out. I am sharing it here again:

using
(SPSite site = new SPSite("YourSiteUrl"))

{
using(SPWeb web = site.OpenWeb())

{
SPFolderCollection folders = web.Folders;

SPFolder folder = folders["DocumentLibraryName"];

SPFile file = folder.Files[itemIndex];

file.CheckIn("CheckInComment", SPCheckinType.MajorCheckIn);

file.Approve("Approved");

web.Update();

}

}

This code snippet can be modified in the way desired.

Thanks,


Rahul Rashu
 

Saturday, 8 September 2012

How to find the size of a subsite in Sharepoint 2010

Hi,

Recently a requirement was posted with me where the site administrators wanted to determine the size of their subsites as well and not only for the entire site collection or content database. The size of the content database can be determined easily and same for the site collection. However there was no direct way for the subsites. Hence I had to find out an alternative way for the same and after some research I found a way. The steps are as below:
1. Open any document library of the site which is one level up in site hierarchy for the subsite in concern.
2. In the ribbon select Library--> Open with Windows Explorer.
3. Now a windows explorer will open pointing to that document library. Now navigate to the site level.
4. This will show all the folder available under the site select the folder that corresponds to your site subsite in concern.
5. Right click and select properties. This will display the size.

I hope this will help you out.

Saturday, 11 August 2012

Powershell Script to list of the documents checked out with version details in a site collection in Sharepoint

Hi,

I received a recent requirement to create a report that provides the details of checked out items in an entire site collection. This report should also contain data for the person whom the document is checked out to. It should also provide the version and if no version exists it should mention the same.

So after playing with powershell for some time I prepared a script that works in exactly the same way.
If you want to extract this report to some .csv file then you need to provide this at the time of invoke itself.
For example:-  .\ScriptName.ps1 >FileName.csv
During the execution it will ask for the site url which you need to provide. It works with both Sharepoint 2007 and Sharepoint 2010 versions.




[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

function CheckedOutItems() {

write-host "Please enter the site url"
$url = read-host
write ("SiteURL`t" + "FileName`t" +  "CheckedOutTo`t" + "ModifiedDate`t"+"Version")
$site = New-Object Microsoft.SharePoint.SPSite($url)
$webs = $site.AllWebs
foreach($web in $webs)
{
$listCollections = $web.Lists
foreach($list in $listCollections)
{


if ($list.BaseType.ToString() -eq "DocumentLibrary")
{
 $dList = [Microsoft.Sharepoint.SPDocumentLibrary]$list
 $items = $dList.Items
$files = $dList.CheckedOutFiles
foreach($file in $files)
{

$wuse = $file.DirName.Substring($web.ServerRelativeUrl.Length)
Write ($web.Url+ "`t" + $wuse+"`/" + $file.LeafName +  "`t" + $file.CheckedOutBy.Name + "`t" + $file.TimeLastModified.ToString()+"`t" + "No Checked In Version" )
}
 foreach($item in $items)
 {
 if ($item["Checked Out To"] -ne $null)
  {
$splitStrings = $item["Checked Out To"].ToString().Split('#')

  Write ($web.Url+ "`t" + $item.Url + "`t" + $splitStrings[1].ToString() + "`t" + $item["Modified"].ToString() +"`t" + $item["Version"].ToString())
 }
 }


}



}
$web.Dispose()
}
$site.Dispose()
}


CheckedOutItems


I hope this will help you out.

Thanks,
Rahul Rashu

Monday, 16 July 2012

How to embed flash files into Sharepoint site peges

Hi,
Recently a requirement was posted to me where a site admin wanted to use some flash files on some of the pages in his site. After some initial look here are the steps followed:

1. The flash file was uploaded to a document library.
2. In the page where he wanted to show the flash file a content editor webpart was added to the webpart zone
3. The following script was added into the source editor to get it worked.
<Embed src="RelativeUrlToFlahFile" TYPE="application/x-shockwave-flash" width="700" height="200"></Embed>

The parameters above are self explanatory and can be changed based upon requirements. This works in both MOSS 2007 and SPS 2010.
I hope this will help you out.

Thanks,
Rahul Rashu

Thursday, 28 June 2012

How to embed PDF and text file contents in sharepoint pages

Hi,

I was presented a requirement where some of the site administrators wanted to show contents from PDF and text files to many pages in our sharepoint sites. The content was either text or images. There is always a way to add this under the page controls and pods but it was not an effective way of doing so since for any change in the requirement it has to be carried out in all pages manually. Hence I used the content editor webpart to display the text. The file was stored in a document library and it was being referred by my content editor webpart so any changes in the file will be reflected in my pages as well. To refer the file in my content editor webpart I used the following script in source editor of my webpart :


<iframe width="560" height="345" src="FileUrl" frameborder="0" ></iframe>


Thats it and it worked out


I hope this will help you out.


Thanks,
Rahul Rashu


Thursday, 14 June 2012

How to delete a timer job using powershell in sharepoint

Hi,
Recently many site admins requested me to provide an easy way to delete a timer job. A timer job can always be deleted via a non documented STSADM command as
STSADM -o deleteconfigurationobject -id guid. However this command is not recommended for production environment. Hence to resolve this issue I created a powershell script that takes only one input as the guid of the timer job and deletes it. Here is the script :

param([string]$jobid)

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")


function RahulDeleteTimerJob($jobid) {

write-host "This script will delete a timer job"
        $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
        $services = $farm.Services
        foreach($service in $services)
{

$jobdefs = $service.JobDefinitions
foreach($job in $jobdefs)
{
       if($job.Id.ToString() -eq $jobid)
{

          $job.Delete()

}

}

}

write-host "The job is deleted"

}

RahulDeleteTimerJob $jobid




I hope this will help you out.

Thanks,
Rahul Rashu

Tuesday, 5 June 2012

How to Hide Columns in list forms in sharepoint without using Sharepoint designer

Hi,

Recently a requirement was posted to me where the end users wanted to hide columns in list forms without using sharepoint designer. Our site admins can do this easily using sharepoint designer but since there were many lists and requests so it was not possible to go to each and every one and carry out this task manually. Hence I created a tool using C#. This works in both MOSS 2007 and SPS 2010.This takes three inputs:
1. The complete List URL. ex: http://webApplication/Site/Lists/ListName/Allitems.aspx. (Note:- The URL upto any view can be used here).
2. The field name that is to be hidden.
3. 1 ,2 or 3 based on the requirement as these numbers stands for "1 for EditForm, 2 for DisForm and 3 for NewForm".

On successful completion it will show a message that the execution is completed.
Here is the code to do this:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace HideFieldInListFormRahul
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("This tool will hide a field from list forms. It requires List Url and field URL");
                Console.WriteLine("Please enter list URL");
                String listUrl = Console.ReadLine();
                Console.WriteLine("Please enter column name");
                String columnName = Console.ReadLine();
                Console.WriteLine("Enter 1 for EditForm, 2 for DisForm and 3 for NewForm");
                String formType = Console.ReadLine();
                using (SPSite site = new SPSite(listUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        String listRel = listUrl.Substring(web.Url.Length);
                     
                        SPList list = web.GetListFromUrl(listUrl);
                        SPField field = list.Fields[columnName];
                        if (formType.Trim().Equals("1"))
                        {
                            field.ShowInEditForm = false;

                        }
                        else if (formType.Trim().Equals("2"))
                        {
                            field.ShowInDisplayForm = false;
                        }
                        else if (formType.Trim().Equals("3"))
                        {
                            field.ShowInNewForm = false;
                        }
                        else
                        {
                            Exception ex = new Exception("No Proper number between 1 to 3 has been entered");
                            throw ex;
                        }
                        field.Update();
                        Console.WriteLine("The execution completed.Press Enter to Exit");
                        Console.ReadLine();
                    }
                }

            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Console.ReadLine();
            }
        }
    }
}

I hope this will help you out.
Thanks,
Rahul Rashu