Wednesday, December 3, 2008

Consuming a Web service that requires Windows credentials

Introduction

When we consume a web service in .NET, the default user credentials passed on is that of the anonymous user.If the web service is configured to allow access to anonymous users, then things will be fine.

But at times the web service will be configured for Integrated Windows Authentication. In such case valid credentials has to be passed.
Failing to pass the correct credentials will result in an error.

In the code shown below the following error will be thrown at the line where the TestWebMethod is executed.
Code:
TestWebServiceClient webServiceClient = new TestWebServiceClient();
webServiceClient.TestWebMethod();

System.Net.WebException: The request failed with HTTP status 401: Unauthorized

Thus, in an intranet environment, it would probably make sense to pass the current windows user’s credentials.
In an internet scenario, where the call is made over the internet, the credentials have to be passed explicitly.

Passing Current Windows credentials:

The property Credentials of the proxy class in the web service client has to be set with the correct credentials.
To set the current windows credentials we use CredentialCache.DefaultCredentials.
Code:
TestWebServiceClient webServiceClient = new TestWebServiceClient();
webServiceClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
webServiceClient.TestWebMethod();

Passing Explicit Windows credentials:

For this the property Credentials of the proxy class in the web service client has to be set with the correct NetworkCredential object
Code:
TestWebServiceClient webServiceClient = new TestWebServiceClient();
System.Net.NetworkCredential objCredential = new System.Net.NetworkCredential(UserName, Password, Domain);
webServiceClient.Credentials = objCredential;
webServiceClient.TestWebMethod();

Tuesday, March 18, 2008

Custom Field type SPFieldMultiLineText




I have created a custom field type based on SPFieldText and realized down the lane that it has a maximum size limit of 255 characters.
So I modified my code to have my field derive from the SPFieldMultiLineText.
But that did not solve the issue - I still got the same error as when I used SPFieldText:

"Invalid text value
A text field contains invalid data. Please check the value and try again."

Thanks to Serge's blog SharePoint: Custom Field Type for MultiLine text, I got over the problem!

Apparently, just have the class derive SPFieldMultilineText is not enough.
The data type of the base field type used must be specified in FieldTypes XML as "Note".

<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">MyTagType</Field>
<Field Name="ParentType">Note</Field>
<Field Name="TypeDisplayName">MyTagType</Field>
<Field Name="TypeShortDescription">Tags</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowInListCreate">TRUE</Field>
<Field Name="ShowInSurveyCreate">TRUE</Field>
<Field Name="ShowInDocumentLibraryCreate">TRUE</Field>
.
.

</FieldType>
</FieldTypes>


"Ahhh! Another SharePoint Ephiphany."
Thank you, Serge!