Gino Abraham's Blog

March 2011 Entries

Powersheel script to Clear all Items in Sharepoint List

The below given powershell script will clear all items in a list. You should use powershell IDE to run this script.

 

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

$siteObj = new-object Microsoft.SharePoint.SPSite ( "http://yousiteurl" )
$SPweb = $siteObj.OpenWeb()
"Web is : " + $SPweb.Title

$oList = $SPweb.Lists["you list name"];

$oList.Title + " : Total Items Count is " + $oList.ItemCount

$collListItems = $oList.Items;
$count = $collListItems.Count - 1

for($intIndex = $count; $intIndex -gt -1; $intIndex--)
{
        "Deleting : " + $intIndex
        $collListItems.Delete($intIndex);
}

 

 

 

Getting Distinct Column Values from a (C#)Datatable

The following code snippet shows you a way to extract distinct values in a specific column from a datatable.
 
            // Datatable Creation
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("Col1");
            dt.Columns.Add(dc);
            dc = new DataColumn("Col2");
            dt.Columns.Add(dc);
 
 
            DataRow dr = dt.NewRow();
            dr[0] = "A1";
            dr[1] = "B1";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "A1";
            dr[1] = "B2";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "A1";
            dr[1] = "B3";
            dt.Rows.Add(dr);
 
           // To Copy distinct values from col1 to a different datatable
            DataTable uniqueCols = dt.DefaultView.ToTable(true, "col1");
 
 
We have got disctinct values of  col1 into uniqueColTable. DefaultView.Totable can accept more than one column. The first parameter indicates that the method should fecth disinct values. you can also write dt.DefaultView.ToTable(true, "col1","col2"); which will fecth distinct values from both the column combination and copy that to new datatable.

Getting Internal Name of a Share Point List Fields

Over the last 2 weeks i was developing a tool to migrate Lotus notes data base to Share point. The mapping between Lotus notes schema and share point list schema was done manually in an xml file for out tool. To map the columns we wanted internal names of each field. There are quite a few ways to achieve this, have explained few below.

If you want internal names for one or 2 columns you can do so by navigating to the list setting and clicking on the column name. Once you are in column's details, you can check the query string of the page. The last item in the query string would be field's internal. Replace all "%5f" with '_' will give you the field internal name.

In my case there were more than 80 columns. I used power shell to get the list of columns with details.

Open windows Powershell and paste the following script after modifying the url and list name.

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

$site = new-object Microsoft.SharePoint.SPSite("http://yousitecolurl")

$web = $site.OpenWeb()

$list = $web.Lists["yourlist name"]

$list.Fields | Format-Table Title, InternalName, TypeAsString

I also found a tool in Codeplex.com which can generate a wrapper class for a list. The wrapper class will give you the guid and internal name for all fields in the list.  You can download the tool from http://imtech.codeplex.com/

Just enter the url in the text box and hit open. All the site content will be listed at the left hand side, expand the list, right click and select generate wrapper class.

 

Error occurred in deployment step 'Retract Solution': Cannot start service SPUserCodeV4 on computer

When i got this error i searched in net and found a solution to enable Microsoft Sharepoint foundation User Code Service in services section of Central Administration. Unfortunately i couldnt find a service with that name.

While searching in services.msc i found that SharePoint 2010 User Code Host was not started. Once the Service was started  the issue got resolved.


 

Issue While uploading a image to share point 2010 picture library.

I was trying to upload a image to my picture library using sharepoint client object model.

I Used the code from the below blog to upload a file to my picture library.

http://blogs.msdn.com/b/sridhara/archive/2010/03/12/uploading-files-using-client-object-model-in-sharepoint-2010.aspx

The image got uploaded sucessfully. But when we took the relative url to update in a different list, we were getting empty image symbol.

After a lot of analysis we figured out that the issue was with the file we uploaded. An image file which is of jpeg quality was uploaded to an application with giff extension.

Try this. Copy a JPG file from net and save it to your file system. Change the extension of the file from jpg to giff.

When you change the file extension the image quality remains same but it will open in picture viewers. Upload the file to your picture library. Once uploaded you will get the file listed as thumbail in your picture library. Click on the thumbnail image it will open up a page showing a larger image with file details.

Now click either on the image or the file name hyper link, it will open up an empty page with default no image symbol.

I wasted a lot of time on this figuring out the issue, so thought of sharing here. Hope this helps some one.