Wednesday, May 5, 2010

Download document from SharePoint library using web service

Hi All,

Many times we come across to a situation where in we need to download the document, PowerPoint files, excel files or just any other file from SharePoint.

However we are not on the server or let’s say we need to show those documents in some ASP.Net application and show their content on some ASP.Net page.

At that time we need to use the web service provided by SharePoint and then we are able to download any document from any document library in the site.

Following is the simple code that you need to write to pull the document to your local machine.

First add the web reference and then add URL of your SharePoint server + “_vti_bin/copy.asmx"”.

Then give it a proper name, so that you can reference in the code.

Then write down simple code shown below. In this example I am downloading simple ppts file from SharePoint server and copying it in C drive. I am passing default credential as I am in the same domain connected with windows authentication. However if you are not connected, then pass the user name and password of administrator who has a permission over the site.

Here DownloadSharePointDocument is the name of my web reference proxy class.

DownloadSharePointDocument.Copy copyService = new EncryptAndDecrypt.DownloadSharePointDocument.Copy();

copyService.Url = "https://intranet.eclipsnet.com/" + "_vti_bin/copy.asmx";

copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

string sourceUrl = “{server url} + {document lib name} + {file name}”;
string destinationUrl = @"C:\\Downloaded.ppsx"; // location where file to be downloaded

DownloadSharePointDocument.FieldInformation fieldInfo = new DownloadSharePointDocument.FieldInformation();

DownloadSharePointDocument.FieldInformation[] fieldInfoArray = { fieldInfo };
DownloadSharePointDocument.CopyResult cResult1 = new DownloadSharePointDocument.CopyResult();
DownloadSharePointDocument.CopyResult cResult2 = new DownloadSharePointDocument.CopyResult();
DownloadSharePointDocument.CopyResult[] cResultArray = { cResult1, cResult2 };

byte[] fileContents = new Byte[4096];
copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContents);
FileStream fStream = new FileStream(destinationUrl, FileMode.Create, FileAccess.ReadWrite);
fStream.Write(fileContents, 0, fileContents.Length);
fStream.Close();


That is it. now you have the document in your machine.

Monday, May 3, 2010

Limitation of SharePoint Designer workflow

Hi All,

We all know the power of SharePoint Designer. When there is any requirement in which we need to send a mail when item is added to list or document library along with its metadata column values and with some formatting.

However I observed one simple limitation of designer workflow. We can add field values in the body part when we want to send an email from designer workflow. However when it comes to the subject line, we cannot add some static text and field value together. It can be wither entirely static subject line or it can be entirely a field values.

So how should we overcome this difficulty? Well, the idea is have one calculated field and define its value with “static text” & [field value] and when sending mail from designer, mention this calculated field in subject line.

You are ready to send a customized subject line.

Hope this post will help you.

---------Updated on 21-05-2010----------------

I would like to highlight few more limitations in Workflow designer.

We do not have facility to add bcc field.

We cannot deploy designer workflow created in local machine to staging or production environment easily.

We cannot set a priority of an email.

There is no HTML editing capability in body text. Hence we are bound to write manually all html tags and formatting stuff. This is really a big limitation I would think.

Let’s say, you are sending an email and then you include field values in the body of the email. If any field has no value, means if the string is empty, Designer workflow inserts “??????” (Yes, six question marks) in that. Now this is really a strange behavior and totally unexpected one.

Sunday, May 2, 2010

The Result is out

Hi All,

We have asked in poll question if you have used and observed any significant change in windows workflow foundation 4.0.

It looks like still people have not started using it.

Friday, April 30, 2010

Limit character length in multiline text box in SharePoint using jQuery

Hi All,

SharePoint really provides many of the features out of the boxes. I would say 75% of feature it provides out of the box; however there are some scenarios where these features cannot help us.

Let me give you a classic example. Recently I got a simple requirement. When we create multiline text box in SharePoint, unfortunately there is no option to limit the number of characters in that. It is available in single line of text field but not in multiline text.

Ok, what is the solution? Yes, we can create custom field and ready to go. Yes I agree. But this will take time to create custom field and deploy on the server which may administrator in the company does not give access easily to upload anything.

Hummm…..So what to do? Answer is use jQuery. JQuery is here to save you.

All you have to do is add content editor web part on the page, and just bind keypressed event of textbox and count the number of characters. That is it. So simple. If it exceeds certain number of characters, you popup some message and then do not allow to enter more character in that.

Sounds great. Isn’t it? I know, yes of course.

At least this is far better than developing custom field type and then installs it in server through WSP.

Make sure that you add content editor web part in New Form as well as Edit Form both.

Let us say I have reason filed as multiline textbox in the list and I want to limit number of characters in that 100. We do not want to allow any more character to type in.

So here is the jQuery that you write. If you know that jQuery syntax, then this is not difficult to understand. Text area is what we are looking for with title Reason as list field.



And there you go, try to add more characters in reason and see the result.



So, doesn’t it save your lot of time to write custom field for such a common requirement? Do share your thoughts

Wednesday, April 28, 2010

Toggle multiline text area in SharePoint using jQuery

Hi All,

Let us explore more in to area of jQuery with respect to SharePoint. When we want some field to not appear in the NewForm or EditForm or so we can make a use of jQuery to hide them.

When we want to toggle the visibility of any field on the form, we can achieve the same with the jQuery.

jQuery has helped a lot in the world of SharePoint.

I’ve created one test list and I have one column named status. There are four entries in that as you can see in below figure.



Now I want to toggle the Reason text area below that. So I’ve written the following code in jQuery. Just add content editor web part and write down the following code. You have to modify your HTML portion based on your field name. Just observe the view source and check the hierarchy level for span, td and tr for the field and accordingly make the change.



Note: One big change. make sure that you write == sign to compare instead of = sign. make this change while writing the code for above figure.

We are handling the change event of dropdown here; you can do the same stuff with checkbox, just make sure to change the change event function to click function. That is the only change required if you would like to go with checkbox.

When I select the drop down and change the value, I get to see text area also toggles.



And when I change the drop down value



You can also use hide () and show () function to get the desired field on the page and use these methods to perform your own functions as per the need.

Tuesday, April 27, 2010

Disable OK button in SharePoint using jQuery

Hi All,

It is really funny but today I come to know the real power of jquery. I know that jQuery is real cool dude in web world. However when I applied some stuff, it got my relief because it solved my two problems.

When you want to disable OK button in NewForm, Editform of list or document library in SharePoint, you can achieve this with the help of jQuery and it helps a lot. Let us say that you have some requirement like if status is suspended then nobody should be allowed to make any changes in the status field.

Let us add complexity to this. We have attached a workflow that triggers on update. Now the problem with SharePoint is even though nothing is updated, then also it triggers the workflow. So if you do not disable OK button and you go and click on edit item. And then when you do not update anything, click on OK. Boom...workflow triggers. Man…I don’t like this.

Here comes jQuery to save us. Disable the status and disable OK button. That will solve the purpose.

What do you need to do? Well, I have created dummy list to demonstrate this.

I have few fields and one of them is status. Statuses are New, In Progress, Archived and suspended.

When the status is suspended, I do not want to allow any of the users to change the status and also there is workflow attached to this list which triggers if we update any item. Workflow is attached from the SharePoint designer. Hence we are even not writing a code in event handler to check for the item has changed or not in ItemAdding event.

So write down this jQuery in content editor web part. Just add CEWP on top with appending &toolpaneview=2 to the URL.

Add following code to the content editor web part. If you observe we have taken reference of jquery from shared document. You can download the jQuery and then give href to the document library path. What we have done in this jQuery is simply finding all drop downs and then checking its value. If it’s suspended, then we are again checking button OK by its class and once found, we are disabling the button. Hummm…Easy ha? Yes it is.



And see the result.



Get ready for some more exciting stuff with jQuery.

Tuesday, April 20, 2010

Adding custom activity to SharePoint designer Part – 1

Hi All,

This is one of the common questions being asked many times, that it is okay that we can develop some nice custom activity and then give it a try to use in different projects and also can give to other developer to work on it.

However what about to all designers who work in SharePoint designing team and know how to work with the workflow? Well, for them we have good news. You can create your own custom activity in the same way we saw in creating custom activities series. The fun part is you can modify the same activity and make that activity available for SharePoint designer.

Well before digging down in to exploring this stuff, let us spend some time understanding how SharePoint designer workflow actually works with activities and actions with SharePoint server.

All activities that you can see in SharePoint Designer come from one DLL named “Microsoft.SharePoint.WorkflowActions.dll”. There is one .ACTIONS file sitting under template folder that actually does the job for you.

This file is responsive for displaying all activities and also responsible for binding parameters to properties for the activities.

All we have to do is create a custom activity in the same way we have created earlier and then one more important stuff to do is create one .ACTIONS file so that SharePoint designer can understand that sentence written in .ACTIONS file and can display our activity in SPD. Also we need to add the assembly of custom activity to GAC by giving a strong name. Final step is to register that activity as AuthorizedType under web.config for the web application we are going to use it for.

We will cover all actions in part 2. Part 1 was more of informative about custom activity with SharePoint Designer.

Sunday, April 18, 2010

Create or Extend web application link not visible

Hi All,

Today I was facing one strange problem. I installed SharePoint and then when I went to Central Administration to create web application, what did I see there? Well, there was no Create or extend web application link exist.

Even if I was logged in as administrator of my machine and the account was showing system account, hence I should get an option for that, however that was not there.

Well, this is the first time I have faced such problem.

Well, time to find some information in internet. And guess what I found 5 different solutions for that. For most of the poeple second trick (mentioned below) out of five solutions did the majic.

Well let me tell you all solutions that I found, so that you can try each one of them one by one.

Tell you what I wasted my 3 hours to figure out and installing service packs and all. Well in my case it was not needed.

My suggestion, go one by one in order that I have mentioned here. Chances are more that problem will be resolved.

1) Open the central administrator from Start menu with run as administrator option.

2) Your central administration site should be on intranet mode not on internet mode. This is the key area and most likely problem will be solved with this. Go to Tools-Internet option-Local intranet-Sites-Advanced and then add your central administration URL. (This did a trick for me and saved my day) Thanks to jonas for this deadly finding. :)

3) If first two combined step cannot solve this problem, then open IE as administrator from start menu to open central administration.

4) Download WSS service pack 2 and SharePoint service pack 2 and then check.

Let me know how much this post saved your time. :)

Friday, April 16, 2010

Working with declarative workflows Part 3

Hi All,

Let us continue our experiment with declarative workflow. If you haven’t read part 1 and part 2, I recommend you read them and enjoy reading further.
Now then we know how to create basic declarative workflow; we will move ahead and create declarative workflow that can accept parameters.

Go ahead and add one more XML file and this time name is “ConditionalDeclarativeWorkflow.xml” and then immediately change its extension to .xoml.



Now add code behind fine and name it ConditionalDeclarativeWorkflow.xoml.cs.

And start adding following stuff and do observe each condition that we have associated with xoml.

public partial class ConditionalDeclarativeWorkflow : SequentialWorkflowActivity
{
private string _BirthDate = string.Empty;

public string BirthDate
{

get { return _BirthDate; }
set { _BirthDate = value; }

}

private void CheckValidBirthDate(object sender, ConditionalEventArgs e)
{
if(_BirthDate == string.Empty) e.Result=false;

try
{
if (Convert.ToDateTime(_BirthDate) > DateTime.Today)
{
e.Result = false;
}
else
{
e.Result = true;
}
}
catch (Exception ex)
{
e.Result = false;
}

}

private void CalculateAge(object sender, EventArgs e)
{
if (_BirthDate != string.Empty)
{
DateTime dtBirthDate = Convert.ToDateTime(_BirthDate);

System.TimeSpan diffResult = DateTime.Today - dtBirthDate;

double age = diffResult.Days / 365;

Console.WriteLine("
}

}


Now how we used WFC in our previous post, we will use the same way here also. Go ahead and open command prompt.

And generate the DLL in this way. This time we are going to give two parameters to wfc.exe, first definitely a xoml and the other xoml.cs file.



Now add newly generated DLL to the reference of console workflow application.

Our workflow accepts the parameter, hence we are going to declare parameters and then invoke workflow.

Console.WriteLine("Waiting for workflow to complete");

Dictionary parameters = new Dictionary();
parameters.Add("BirthDate","12/03/1982");


WorkflowRuntime runtime = new WorkflowRuntime();
WorkflowInstance instance = runtime.CreateWorkflow(typeof(ConditionalDeclarativeWorkflow), parameters);

instance.Start();

Console.WriteLine("Workflow completed...");

Console.ReadLine();




So we have given input and we got the result, now let’s pass incorrect birthdate.



That is it. Hope you will try more complex example.

Wednesday, April 14, 2010

Working with declarative workflows Part 2

Hi All,

Working with declarative workflows Part 1 was all about understanding the basics of declarative workflow. In this post, we will make our basics bit more strong by doing one more practical and almost the same stuff, but with little change.

Take the same example that we took in last Part 1. However before continuing further, let us explore one option that comes handy with workflow. Open “C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin” folder. It may differ from OS to OS. So just make sure that you are under bin folder of Windows.

And observe one exe named “WFC.exe”. This is the command utility that will convert our declarative XML into DLL.



But hey why again DLL? So the answer is DLL execution is faster than XML execution. So you can also try this option after creating your workflow with declarative manner.

One other important note to understand is that when we will convert our declarative XML workflow using this utility, it will not work directly because this utility requires file which has xoml extension. So go ahead and open the declarativeworkflow.xml and rename it to declarativeworkflow.xoml.

Also observe one more change in declarativeworkflow.xoml this time. We need to add one more attribute and that is Class attribute. This is required because after compiling into the assembly, workflow utility will give this name to the generated DLL.

<SequentialWorkflowActivity x:Name = "Workflow1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow"
x:Class ="DeclarativeWorkflow">
<SequenceActivity x:Name="SequenceActivity1">
<DelayActivity TimeoutDuration="00:00:15" x:Name="DelayActivity1"></DelayActivity>
</SequenceActivity>
</SequentialWorkflowActivity>


Now open the command prompt and navigate to the directory till your project is. Infect navigate to the folder where we have this xoml file sitting.



And then just type in the path for WFC.exe and give our xoml file name as a parameter and hit enter and see the magic. You will see no error and no warning and will get DLL as in output.



Now go ahead and open your console workflow application and add the newly created DLL as reference.



And then run the workflow and see it in action.



Read Working with declarative workflows Part 3 for further reading.



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