Friday, October 24, 2008

Removing or Hiding workflow column from View of list or document library

Hi All,

Every time i work with workflow and everyday i got new challenge in that. well, of course without challenge, There is no point to work. isn't it? OK, so let's get started.

When you attach a workflow to the list and when workflow gets triggered, you can observe one thing. the workflow name that you have given in the list will come in to the picture. this will appear as one of the field in AllItems.aspx. Woh!!! where did it come from??? Well, answer is, it comes automatically.

Our main aim is to Remove or Hide workflow column from View of list or document library.

See the steps below :

Step 1 : Attach a workflow and name it. make it auto start when new item added.



Step 2: Observe the column appearing automatically in view when new item is added.




So , now you have this column appearing in AllItems.aspx or which actually most of the people do not want to see. yes that is true that you can always go to modify this view and hide that column. but what if there is a scenario where there is no direct access to Modifying view or AllItems.aspx.

Let's say you make webpart which fetches data from AllItems but filtered data and filtration in such a way that it cannot be done by simply applying the filtration in View. so we make separate webpart for it. now even in that new webpart the new column (workflow column) starts appearing. Note one thing, you will not see this column in Fields Collection of that list or document library, is automatically generates and attaches to the view. so forget about this. After this workflow field gets generated, you can see it in View settings but not in the field.

Hiding that column also has some importance is that you do not want to allow the people to go on a page where they can see that task is assign to which person, or they can just simply go there and perform anything. Ultimately, there should not be an access to the column, so better to hide that. but simple it cannot be done.

All you need to do is play with Viewfield of the SpView. you do not have to do anything with the SPList here. because SPList Fields do not contain this workflow column. Now interesting thing is how will you get the name of that column in the code so that you can hide it from the ViewFields? Well , if you are thinking that name that you have given to the Workflow while attaching it with the List or document Library, then let me tell you, this is totally wrong.

Yes, even i was surprised to see this. but then i had to work on it, so i just took a look on ViewFields of the View and i come to know an interesting thing that the column name of that workflow in the ViewFields is first 8 characters, first 8 letters of the name that you have given. Even if you give space in between. for Example, the name that you have given while attaching the workflow is "Custom Workflow:, then that field in the ViewFields will be named as "CustomWo", see no space in between and same case as you have given. means if you have given "cUsTOm wOrkflow", then the name of the field in Viewfiels will be "CUsTOmwO". so be aware of it. to be on safer side, compare by applying .ToUpper() both the side and then perform the task.

Here is a way to remove the workflow field that appears automatically when workflow is triggered. Keep in mind of allowing unsafe updates on Web to true and false respectivly after finishing your job done.

First take a SPView object referring your list.

SPView objView = web.Lists["List_Name"].Views["View_Name"];
SPList objList = web.Lists["List_Name"];

if (objView.ViewFields.Exists("8_character_name_of_workflow_column"))
{
objView.ViewFields.Delete("8_character_name_of_workflow_column");
}

objView.Update();

Step 3 : Demonstrating from figure



Step 4 : Time to see the result...



See, now that field which was automatically generated is no more available in that View.

Let me brief you about the other way to get the Name which you have to place in the code to remove it from the View. to be on safer side, follow this pattern, even what i mention is very correct.

All you need to do is generate the schema file of the list or document library and search for the following Tag lines.



You can follow this approach as well.

I hope this will make the whole picture clear. This will be very helpful when you do not deal directly with List but uses them in some webpart and uses the views of that list. example to give is ListViewWebPart.

That's it. your job is done. now that column will not appear in the view.

Thursday, October 23, 2008

Remove link on Lookup Field

After creating List view webpart programmatically we are facing another problem.

For creating list view web part programmatically please check this link.

Scenario:
we have a Master list were the master data like Types, Departments this kind of value are stored.

As we are using custom list view webpart and using lookup filed for this data.

Now our problem is removing link of the lookup fields because it will redirect the user to that master list which we don’t want.

We are also using some other lookup fields which are referring to the title of some other list like contact name or company details etc... But we have to keep that link.

So simple problem statement is
1) remove link from lookup field
2) But keep link of lookup which are mapped with LinktoTitle.

Solution:
We found that if you are selecting any look up field then other then LinktoTitle then your lookup URL will have something like this.
<<your site>>/Lists/<<List name>>/DispForm.aspx?ID=<<ID of that field>>&RootFolder=*

So we can find out that if Link contain RootFolder=* then it is referring to the lookup.

To removing this, what we had done is that something unethical.

We don’t know whether this is correct way or not.
We resolve this problem from java script.

In java script we find all the Anchor element of the document.

Then check the index of href attributes “RootFolder=*” so that we came to know that in this page this field is lookup field.
Just call this java script on the page where you want to remove the link of lookup.

You can use content editor webpart also.

Here is the java script to remove the lookup links.
<script language="javascript" type="text/javascript">

_spBodyOnLoadFunctionNames.push("RemoveLookupLinks");
function RemoveLookupLinks()
{
var oP = document.getElementsByTagName('a');//the collection of <a> tags
var flag = false
for(var i=0;i<oP.length;i++)
{
if(oP[i].attributes["href"].value.indexOf("RootFolder=*")!= -1)
{
var linkvalue = oP[i].innerHTML;//value of the lookup field
oP[i].parentNode.innerHTML = linkvalue;//replacing value of the lookup to whole the Anchor tag
flag = true;
break;
}
}
if(flag)
RemoveLookupLinks();
}
</script>

Check it and let us know that it is working for you guys,

We have not done it for LinkToTile lookup link because the problem with RootFolder=* is that it will not redirect back to this page while LinkToTitle will redirect to the same page from where you traverse.

It solves our problem for the time being (until another requirement will come)

Monday, October 20, 2008

MOSS List Open Item in New Window

I would like to start article by first thanking http://mindsharpblogs.com/ for providing a wonderful utility.

We can use the following simple steps to open a item in new Window when someone clicks on a Document in a Document Library or clicks on a List Item in a List in MOSS inspite of clicking "Open in the new window".

First of all download DWP file unzip it and save it in your system
Browse to the ListView page of a SharePoint List or Document Library.
Replace the URL in your browser's address bar with the following text and then press (the Import ToolPane will show when the page posts back ):
javascript:MSOTlPn_ShowToolPane('5');
Click the Browse... button, locate the DWP you saved in the first step, and click the Open button.
Click the Upload button.
Drag and drop the Web Part into the Web Zone below the existing Web Part.
That's it! It works on nearly every SharePoint List Type (including Document Libraries with the exception of Picture Libraries).

Friday, October 17, 2008

Sharepoint View Comparison

For one requirement we need to compare particular view with another view (let’s say default one) to check that the first view has been modified or not.

So for that one found two perspectives.
1) Compare the view fields only.
2) Compare the view fields with order also.

For that we found one good article from Adam Buenz

But I found the other ways.
For second approach we have a smart and easy way

Compare the view fields with order.

For that we just need to compare SchemaXml of ViewFields property.
Here is the snippet
private bool CompareViewWithOrder(SPView FirstView, SPView SecondView)
{
if (FirstView.ViewFields.SchemaXml == SecondView.ViewFields.SchemaXml)
return true;
else
return false;
}



Compare the view fields without Order.

If order doesn’t mean and you just want to compare the fields of the views then here is the snippet.
private bool CompareViewWithoutOrder(SPView FirstView, SPView SecondView)
{
string[] strarrFirstViewFields = new string[FirstView.ViewFields.Count];
string[] strarrSecondViewFields = new string[SecondView.ViewFields.Count];
FirstView.ViewFields.ToStringCollection().CopyTo(strarrFirstViewFields, 0);
SecondView.ViewFields.ToStringCollection().CopyTo(strarrSecondViewFields, 0);
if (strarrFirstViewFields.Length == strarrSecondViewFields.Length)
{
for (int i = 0; i < strarrFirstViewFields.Length; i++)
{
bool bFieldFound = false;
for (int j = 0; j < strarrSecondViewFields.Length; j++)
{
if (strarrFirstViewFields[i] == strarrSecondViewFields[j])
{
bFieldFound = true;
break;
}

}
if (!bFieldFound)
{
return false;
}
}
return true;
}
else
return false;
}


So these are the way for comparing the SharePoint views with fields and order of the fields.

Thursday, October 16, 2008

How to access personal views form Sharepoint Content Database

Each list or library has at least one view, based on its type and which settings have been applied. Some lists and libraries have other built-in views, and you can create custom views. For example, a task list has several views, such as just the tasks due today, just the tasks assigned to you, all tasks, and so on. You can create personal views and public views.

A personal view is available only to you when you look at a list or library. So functionally only the user who has created the Personal View can see this view. Not even the System Administration can view the personal view.

In technicial terms a view consists of the following:
View Fields – Fields that will be displayed in the View.
Query – CAML query that fetches the records from the list.
Paging – True/False
Row Limit – Number of records to be shown.

Example:
<ViewFields><FieldRef Name="Attachments"/><FieldRef Name="LinkTitle"/></ViewFields><Query/><Aggregations Value="Off"/><RowLimit Paged="TRUE">100</RowLimit>


The way that we have found out to have a look at these attributes of a view (Personal / Public) is to drill thorught the content database.

Open the WSS Content Database. There is a Table dbo.WebParts
Run this query on the table.
select tp_View from webparts
where tp_displayname like '%My View%'



where between ‘%%’ there is My View.
This is the name of the personal view I created.
Explore the table and the columns and you will get more to learn.

Running two versions of workflow at the same time

Hi All,

Versioning of Workflow is something very interesting to me. I came across to a scenario where i needed to run two versions at a time.

Let me explain you the scenario and making you understand about versioning in workflow.

Versioning is not directly supported in MOSS and in workflow. you have to manage it. it is not like you place the different version numbered assembly and program start using it. no this is not the case for this. here it is a different trick.

When you build the workflow deploy it in production environment, there might be many number of workflows running in the system. you cannot change them directly because then otherwise it will stop the work. so this cannot be done. all you have to do is you have to make the different versions of it. Now different version does not mean different Version number because that will also not make any sense as it will be entirely a new workflow because you will have to change the File Assembly reference where ever taken in the program like workflow.xml file or any other association and initiation form. so this cannot be done.

So what is the solution to have two different versions of workflow running at the same time?

You have several business rules running in workflow and if workflows are already triggers in production environment you cannot change them. you might have several corrections and change in the existing workflow. but that will then affect the existing workflow which should not be actually.

When you go to the list settings or document library settings and then workflow settings, you can see your attached workflow for that list or document library. when you click on the remove workflow, you will find three options. (1) - new (2) -No new instance (3) remove workflow. Here No new instance makes sense. it means currently attached workflows will not trigger now onwards on this list. here there is only one limitation is that you cannot have the same name of workflow to have two instances running for it. so you need to change the name only. so in workflow.XML just change the name of the workflow and then redeploy it. so hence you will have that workflow as well. so select that workflow add it to the same list or document library and then allow it to run a new instance.

so here what we did is we stopped the already running instance of the workflow and we've attached a same workflow with only name change. so all existing workflow will not get affected and from now onwards new workflow will only gets triggered which will have new business rules or any change.

That's it. your job of running two different versions of workflow is done.

Wednesday, October 15, 2008

Query the List by Look up field id

In one scenario we have a same data in look up and when we are querying on that lookup list by value it will give us first value only.

Here is the scenario
We have a contact name as title column and two contacts can have same name.
Now we are referring this title field of contact list to another list named company.

So if we don’t know which contact we are selecting.

And after selecting when we want to find other information from contact list then if we query look up field by text then it will return first entry only.

So chances are there that we are getting the miscellaneous records.

So for that first we have to find ID if selected Look up field
Here is the post how to find it.

Now I want to query the list by Look up id.
I came across the this sites and also want to thank for their suggestion
Adis Jugo
Abstract Spaces

Here is the CAML query for the same

By text value
<Query>
   <Where>
      <Eq>
        <FieldRef Name='Contact' />
        <Value Type='Lookup'>Patel, Parth</Value>
      </Eq>
   </Where>
</Query>


By ID
<Query>
  <Where>
      <Eq>
         <FieldRef Name='Contact' LookupId='TRUE' />
        <Value Type='Lookup'>15</Value>
      </Eq>
   </Where>
  </Query>


This is working fine for us in all the list except one.
And it is not working in the list which has around 40 Lookups

Then we came to know that from Abstract Spaces
If you have lookups more than 16 then it will not work it. If you query on 17 or 18 or further look up it will query again on first or second and so on…

The site also says it is already log on tech net but I cannot find it. If anyone find such article then please give a link in comments.

How to get look up column value with ID?

Hi guys,

Recently we are facing problem that how to get/retrieve direct id of look up field from the list (for multi select and single select both).
For implementing this we are facing some challenge.

We are using SPQuery are fetching data from a list.

If the list contains Look up field then you observed that
1) If the look up is single select then it will show you the text value of the referenced field( means text of the field)

2) If the look up is multi select then it will show you text value and ID of that list with “;#” separated but the problem is it will not give you ID of first field.

Most of the time we are using this method
SPList spList = spWeb.Lists[strListName];
SPQuery spQuery = new SPQuery();
spQuery.Query = strQuery;
SPListItemCollection spItemCollection = spList.GetItems(spQuery);
if (spItemCollection.Count > 0)
{
dtResult = spItemCollection.GetDataTable();
}


But the return data table has the problem mention above.

So after checking a lot on the net we cannot find anything.
Then after checking with reflector we came to know that SPListItemCollection.GetDataTable() method will check the field type, if type is lookup or user then it will remove first ID and “;#” from the field result.

So sadly we cannot do anything directly with this method.

But we can find the ID;#Value by SPListItemCollection only

SPListItemCollection spItemCollection = spList.GetItems(spQuery);

String strLookUpValue = Convert.ToString(spItemCollection[0][“Look up field display name”]);


So this way you can find out what you are looking for.

Here is the snippet for the same.
SPList spList = spWeb.Lists[strListName];
SPQuery spQuery = new SPQuery();
spQuery.Query = strQuery;
SPListItemCollection spItemCollection = spList.GetItems(spQuery);
if (spItemCollection.Count > 0)
{
dtResult = spItemCollection.GetDataTable();
for (int i = 0; i < spItemCollection.Count; i++)
{
for(int j = 0;j < spItemCollection[i].Fields.Count;j++)
{
if (spItemCollection[i].Fields[j].Type == SPFieldType.Lookup && !spItemCollection[i].Fields[j].ReadOnlyField && !spItemCollection[i].Fields[j].Hidden)
{
dtResult.Rows[i][spItemCollection[i].Fields[j].InternalName] = Convert.ToString(spItemCollection[i][spItemCollection[i].Fields[j].Title]);
}
}

}
}


Please keep in mind this method will cause performance issue if your query returns bulk data but will be very usefull for specific query where data can be countable on fingers. :)

Tuesday, October 14, 2008

Sharepoint List View Style - Preview Pane

Sharepoint List View has the ability to show its information in various Styles
like Preview Pane, Boxed, Newsletter etc.

With the help of these styles you can give a face lift to the Data shown.
These Styles can be applied to a List View in the following way.

We have a List “Preview Stype Demo”.



We will apply the Preview Panel Style to the “All Items” View of this List.
Click on All Items Drop Down --> Modify this View. This will open the Page Edit View: Preview Stype Demo.



Select the Style “Preview Pane” and Press OK.

You will see a wonderful style applied to the view.



In the same way you can apply other styles.
The “Boxed” Style will give you the result as below:

Hide / Remove Title column from Sharepoint List

Hi All,

Many times i thought that there must be a way to get rid of this Title column, because it's name does not suit the business requirements most of the time.

so i think best option is to rename the Title column so at least user can feel some good look about that field. Although that is different thing that in code , we still have to refer that field as Title column only.

Is that a best solution? i do not think so. so i though of digging a bit in content types and all and found out one interesting thing.

After digging in this at some extent, i got a conclusion saying that Title column can be considered as a content type column. let me explain you why and that actually gives answer to the post also. Follow the steps mentioned below.

Well here we go :

(1)Go to Your list settings on which you want to hide/remove title column.
(2)Go to advanced settings of that list.
(3)Click on yes at very first option. Allow management of content types.
(4)Once you do this, one more setting panel becomes visible in advanced settings options. check out the figure below.



(5)Now click on that Item content Type.
(6)As you can see here you will find all columns are listed along with Title column.
(7)Click on that Title column. you will see a page similar to this.



(8)Select the last radio button which is Hidden.
(9)save all settings and then go back to list and click on new Item and see... now Title column is no more there....

Make sure that you also remove all reference of Title column from views because it is hidden now but still it will be there in views.

That's it. your job is done.

Wednesday, October 8, 2008

How to create a view based on workflow status column

Hi All,

I came across to one requirement where i needed to show only Completed Workflow Items. That means i have one workflow attached to a list and when workflow is completed then only i have to show that item.

so i though of creating a view for that. View will show only those items for which Workflow is completed, not In Progress Or any other. so i though of filter on that workflow field.

I tried to create view. So i went for create view, i gave name of view and defined filter condition saying that Field is equal to "Complete". and press OK. but when i came to the view, i did not find any items.

I googled a bit, and then come to know that actually these fields are stored as numeric values. Here are the numeric values for different workflow statuses.

In Progress - 2
Complete - 5
Canceled - 15
Approved - 16
Rejected - 17

So now you must have got the idea, i went back and changed it to workflow field equal to 5 and it worked!!!!

That's it. your job is done.

Thursday, October 2, 2008

Backup and Restore Site Collection in MOSS

Hi all,
Here are the stsadm commands that will be useful to backup and restore site collections in MOSS.

Backup Site Collection

First go to command prompt and set the following path:
c:\program files\common files\microsoft shared\web server extensions\12\bin

This is the folder in which the stsadm.exe is present.
After setting this path write the following command:

stsadm -o backup -url http://servername:portnumber/sitecollectionName -filename pathofDestinationFolder/NameofBackupFile

You will need proper rights on the content db to take backup

Restore Site Collection

Set the path as shown in the above example.
After setting the path the process is:

I am taking an example of restoring collaboration portal. Before executing the restore command create a web application and create a site collection with collaboration portal as site template. Here is the command:

stsadm -o restore -url http://servername:portnumber/sitecollectionName -filename pathOfBackupFile -overwrite

writing -overwrite is mandatory else it will give error.

Again you will need proper rights on the content db to execute restore command.

Happy Administering!!

Wednesday, October 1, 2008

Activating and Deactivating Features Programmatically

Activating and Deactivating Features Programmatically
Sharepoint Feature Manager.

You can activate/deactivate features using command-line utility called STSADM.EXE. Another approach is to do the same thing using the rich object model of WSS.
Hats off to Patrick Tisseghem for sharing such a wonderfull article. You can get the article here.

This article is good and easy to understand! Patrick Thanks for sharing.

http://www.u2u.info/Blogs/Patrick/Lists/Posts/Post.aspx?ID=1567

I have added an additional functionality in the utility. This functionality will automatically search for all the sites/sub sites on the Sharepoint Server where this utility will be run.


I have also created a solution for the same. You can download the same from here

http://www.codeplex.com/SharepointFeatureMan/Release/ProjectReleases.aspx?ReleaseId=17847

SharePoint Feature Manager:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com