Showing posts with label Web services. Show all posts
Showing posts with label Web services. Show all posts

Tuesday, May 22, 2007

Sharepoint Web services - UpdateLists gotcha

After some success with Sharepoint Web services, adding fields to a list programmatically, I wanted to be able to also update and delete them from the same program.

Microsoft's sample code (which works ok) in both the WSS2 and WSS3 examples shows your being able to add 2 fields using the XML new field node definitions as follows:










HOWEVER if you try to delete or update these new fields you've added you get all sorts of nasty and unhelpful errors back saying that the web service won’t do what you’ve asked it.

After 4 or 5 hours of head scratching I eventually figured out that because the MS examples create fields that are derived from a BaseType ie FromBaseType="TRUE" - you can't update or delete a FromBaseType Field. (At least I can't find a way to do it - and it would seem to be a bad thing to do anyhow!) (link here to SPField.FromBaseType defn.)

So if you want to programmatically add new fields to Sharepoint lists, and you want to update or delete them afterwards DO NOT include the attribute FromBaseType="True".

This is an example of an XML new field node definition which can be edited or deleted programmatically.

Tuesday, May 15, 2007

Web services deployment problems (dotnet 2.0 and VS2005)

It is very easy to write and debug the web service using VS2005, however deploying that web service to IIS has so many issues to trip you up that I'm documenting my experience here so I can remember all of them.

Firstly create your web service - The VS code out of the box will do. ie
New project->Visual C#->ASP.NET Web Service Application

This will give you code similar to the following:
namespace SimpleTestWebService
{
[WebService(Namespace = "http://iwaszko.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}


Run this code and envoke the http Post to test all is well.

Now choose File->Add new project->Other Project Types->Setup and Deployment->Web Setup Project

Now Right click on the WebSetup project and choose Add->Project Output














Make sure that the correct WebService project is selected and select Project Output, Debug Symbols & Content Files





Then Build the WebSetup project - (Depending on whether you Build in Debug or Release, it will put its setup.exe and websetup.msi in the same named directory)

Note: you can Install or uninstall the web service from VS by right clicking on the WebSetup project in solution explorer and choosing install/uninstall.





Once I installed the web service to IIS these are the issues I had:

1) Make sure you set your IIS site/virtual directory to use ASP.NET 2.0 if you are using VS2005 - see this post for examples

2) Accessing http://localhost/websetup/service1.asmx gave me the following very unhelpfull error:
Failed to access IIS metabase (see link)
This relates to an error where the local machine ASPNET cannot access the IIS metabase. It can occur where DotNet 2 has been installed after IIS and lots of other combinations.

Workaround: Microsoft published several fixes including using asp_regiis -ga ASPNET, however the best solution I found was to go to control panel-> add/remove programs and choose repair on the DotNet 2.0 install.

3) After the dotnet 2.0 repair succeeded, accessing the web service gave me a new error:


HttpParseException, Could not create type 'Service'. (see link)


The solution seems to be that when the package is deployed to the web server, the bin directory is created but the dll's are put in the directory above. Moving the dll to the bin directory manually seemed to fix this error, however I found a setting in the VS2005 deployment project which corrects this behaviour.


Select the Properties for Primary Output from the WebSetup project. Under the Folder property choose bin. This then seems to put the dll's into the bin directory and the web service works correctly.

Why such a simple thing such as deploying a web service seems to be so difficult is something I very much hope Microsoft is addressing with Orcas, however in the meantime I hope that these notes assist you in not having to struggle quite as much as I did.




Infopath 2007 sequential counters

I needed to create an Infopath form which would create a sequential invoice number. I am publishing the form to a MOSS Forms library and using the browser to edit the form.

Easy, I thought... I'll just connect a second data source to a SQL server stored procedure to give me the next invoice number - BUT this seemed to break the browser compatibility no matter what configuration I tried.

This is the SP code - (crude but effective for what I needed)
ALTER Procedure [dbo].[NextInvoiceNumber]
@NextNumb INT OUT as
begin
update lookup
set NumValue=NumValue+1
where Header='InvNumb'
select @NextNumb=NumValue from lookup where Header ='InvNumb'

So attempt two was to use Web Services. (I am new to these but used to do a lot of COM+ distributed applications 0 so how difficult can it be?). Well it turned out very easy to write and debug the web service using VS2005, however deployment was an absolute nightmare.

Ive documented solutions to all the web service deployment problems I found in this post