Saturday, July 16, 2011

Move and Copy operations in SharePoint Lists

Do not know how many of you aware of these important points.

  1. When you move a document/list item from one document library/list to another then the versioning will also be retained in that destination library/list.
  2. When you copy a document/list item from one document library/list to another then the versioning will not be copied to the destination library/list.
  3. When you move a document/list item from one document library/list to another then the metadata will not be copied. To do that you should have same columns in both libraries/lists and they should use same content type [Means they should have same schema].

How to hide a specific div in SharePoint 2010 dialog

The class in styles "s4-notdlg"[means don't show in dialog not+dlg] is what we have to use to not show a specific division on SharePoint 2010 dialog.

For example, there is a banner in the header position of the master page and you do not want to show the banner in the dialog then the only simple option for you is using a simple css class to your banner div. If you add this class to your banner then when no dialog on the page no change to your implementation but when you are on dialog then the division with the class "s4-notdlg" will be forcibly applied to "display:none".

So, the moral of the story it is a good tactic that Microsoft SharePoint team developed to solve some major issues in design on dialogs.

Saturday, July 2, 2011

Upgrade SharePoint 2007 Visual Studio projects to 2010

This will be very helpful for the scenarios where we have custom solutions developed in Visual Studio 2008 for SharePoint 2007 environment and upgraded 2007 site to SharePoint 2010 and now in SharePoint 2010 we like to do some changes to that custom solution files. Simply, we have a SharePoint 2007 site upgraded to SharePoint 2010 and then it has some custom solutions developed like web parts, solution packages, features etc. Now, we are in SharePoint 2010 environment and like to extend the custom web parts, features from the earlier version of SharePoint. So, we need some sort of support to migrate our SharePoint Visual Studio projects from Visual Studio 2008 to Visual Studio 2010 to deploy to SharePoint 2010. I believe you all are clear till this point.

Get the tool here. http://archive.msdn.microsoft.com/VSeWSSImport/Release/ProjectReleases.aspx?ReleaseId=4183

It is simple project and when you build it, you will get executable and installs template to Visual Studio. But, the only preliminary requirement here is, you have to install Visual Studio 2010 SDK to open project.

Once everything is ready, you will see a new template named "Import VSeVSS project" under new project category. Here you go.....

SharePoint workflow staus codes

This is very important for SharePoint devs. Even I have used them many times, but I forget the status codes almost every time. So, might be happening to everyone too and planned to blog in my blog.

The enum SPWorkflowStatus in the namespace "Microsoft.SharePoint.Workflow" in the library "Microsoft.SharePoint" is what we have to use for these status codes.

0
NotStarted
Not Started
1
FailedOnStart
Failed On Start
2
InProgress
In Progress
3
ErrorOccurred
Error Occurred
4
StoppedByUser
Cancelled
5
Completed
Completed
6
FailedOnStartRetrying
Failed on Start (retrying)
7
ErrorOccurredRetrying
Error Occurred (retrying)
8
ViewQueryOverflow
--
9-14
Unknown
Unknown
15
Cancelled
Cancelled
16
Approved
Approved
17
Rejected
Rejected

Hope this is enough for us to get what we needed.  :)

Error occurred in deployment step ‘Recycle IIS Application Pool’: The communication object, System.ServiceModel.InstanceContext, cannot be used for communication because it has been Aborted

When we are developing custom code through Visual Studio 2010 for SharePoint 2010 sites and when we try to deploy the solution then we might see the below error.

"Error occurred in deployment step ‘Recycle IIS Application Pool’: The communication object, System.ServiceModel.InstanceContext, cannot be used for communication because it has been Aborted."

When I see this very first time and did not get any clue of what it is looking for and why it is not able to communicate to SharePoint sites. After doing some trail and error methods and checking Event Viewer, Logs etc... failed in all the ways in deploying the custom piece to SharePoint. Finally, a simple solution worked but didn't find any reason what is the main issue!

Solution:
  1. Sometimes when I do rebuild the project and deploy directly without doing "Package" from Visual Studio it is successful. 
  2. If it is not successful then the only way I see is RESTART your visual studio and deploy.
  3. (From below comments: ) Restarting "Windows Management Instrument" service will work. (I haven't tested).
Frustrated hours come to an end... There is some way always to make our life go smooth! Happy programming.

Thursday, May 19, 2011

Get bytes from Stream in c#

I know you may think what is the need of this post as this is very minor or simple to think and write. But, whenever I need to get bytes from a stream object, I always forgot it. This is simple but most of the times we used to Google. So, planned to write it to remember at least myself next time.
public byte[] GetBytesFromStream(Stream stream)
{
     byte[] buffer = new byte[16 * 1024];
     using (MemoryStream ms = new MemoryStream())
     {
        int read;
        while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
        {
           ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
     } 
}
 

Check list exists in SharePoint site

This is very generic issue all SharePoint developers face at initial stages. As there is no default method available in SharePoint API to check whether if list exists in the SharePoint site, everyone write the below code:
SPList list = web.Lists["Task List"];
If(list != null)
{ 
    //Some code on list.
}
This is not correct to code like this. We all know generics and collections in c#. SPWeb.Lists is a collection and if you want to get one object from collection either we need to pass index or the key. If that didn’t find in the collection it gives us the exception. So, in our example if the Sharepoint site don’t have list named “Task List” then you will give run time exception in the line 1 itself. So, there is no point of checking whether list object is null. So, here is where many people stuck at. As Lists is plain collection object there is no other way of checking for the list exists in collection other than below.
private static bool ListExists(SPWeb web, string listName)
{
try
{
SPList list = web.Lists[listName];
}
catch
{
return false;
}
return true;
}
I know what you are thinking [Is this solution right?]. Yes, unfortunately there is no other way. So, we have to use this to check whether list exists in a SharePoint site.

Saturday, April 16, 2011

How to go to SharePoint Webpart Maintenance page for any page

This is one of the great tips we have in SharePoint. There are many scenarios why we need this. I had very tough time to migrate a SharePoint 2007 site to SharePoint 2010 site. What happened was,
  1. The other team deployed a custom web part 2 years ago.
  2. Used that web part on different pages in that site.
  3. They have taken the site template from the option "save site as template".
  4. After an year, they have removed  that web part from the site collection.
  5. But, with the same site template they have taken in the step #3, they created few sites in the same site collection after they removed the web part from environment.
  6. Remember, the site template still has the references to that custom web part. So, when I try to migrate it always fails..
Sometimes it is very difficult to find what is causing the problem. The sites created were not published sites. So, I cannot go to Edit Properties of the page and go to web part maintenance page from there. As the default.aspx page is directly in the root of the site/web I do not have any other choice to go to web part maintenance page. After a long research I found from Google that the querystring ?contents=1 will do job for me.

Resolution:
In SharePoint, for any web part ASPX page if you append the querystring ?contents=1 then it automatically redirects to the web part maintenance page.

Example: 
For the SharePoint site page
http://mossurl/default.aspx the web part maintenance page url is
http://mossurl/default.aspx?contents=1

Sunday, January 16, 2011

Clear and then disable controls in infopath

This is simple for reading, but difficult to implement in infopath forms. To disable controls we have to use “Conditional Formatting” option of a control. To clear content of a control then we have to use “Rules” option of a control. But, the sequence of execution of these two causes some problems. Take below scenario.
I have a table and each row the first column contains a checkbox and all other 4 columns are having date field, textbox, drop down and date field respectively. Now, the logic should be this.
  1. The default state of checkbox is selected.
  2. When user enter some data in all other 4 fields and now he thought the row should not allowed to enter data then he deselect the checkbox. When user deselects then the first thing should happen is clear the content in all the 4 controls and then all controls should be disabled.
  3. When again user select the checkbox then the controls should be enabled.
This is what to be happen and the first trail when I tried to implement I did below things.
  1. On all other 4 controls I added a rule that when checkbox selected state is false then set the current field to empty.
  2. And then I added a conditional formatting on them to disable when the checkbox state is unchecked.
I did deploy them to SharePoint and when I tested, surprisingly they are not working as expected. The controls are going to disable state but not clearing content in them when I deselect the checkbox. And researched and found that the Rules are not executing. [Didn’t find the reason yet.] And then thought about for alternatives and came up with reverse way. That is, applying rule on the checkbox instead of other controls.
Earlier, I have applied rules on the each and every individual control which needs to be cleared based on checkbox state – Which doesn’t work. Now, I have applied the same logic but applied rule on the checkbox [As there are 4 controls, 4 times I have added set field to empty] and which is working like charm.
So the conclusion I want to tell to you is, when I apply conditional formatting and then rules something is causing problems in the sequence of execution. So, depends on what your control, rules needs to be executed apply the rules on that control only. That should work perfect.