Beginning SharePoint 2010 Development: Fix for Chapter 2 Web Services Soap Exception

A couple of you have experienced a Web service call error (Unauthorized Exception) in Chapter 2. The exception looks like the following:

System.Web.Services.Protocols.SoapException was caught
Actor=""
Lang=""
Message=Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerExcepti on' was thrown.
Node=""
Role=""
Source=System.Web.Services
StackTrace:
at System.Web.Services.Protocols.SoapHttpClientProtoc ol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtoc ol.Invoke(String methodName, Object[] parameters)
at WPFListApp.MYSPWebReference.Lists.GetListAndView(S tring listName, String viewName) in C:\Users\Administrator\documents\visual studio 2010\projects\WPFListApp\WPFListApp\Web References\MYSPWebReference\Reference.vb:line 262
at WPFListApp.MainWindow.btnUpdate_Click(Object sender, RoutedEventArgs e) in C:\Users\Administrator\documents\visual studio 2010\projects\WPFListApp\WPFListApp\MainWindow.xam l.vb:line 68

I just wanted to post an amendment that may help you get through this sample. Replace the commented out line of code below with the other two lines of code where you’re setting UseDefaultCredentials and PreAuthenticate to true.

/* myListService.Credentials = System.Net.CredentialCache.DefaultCredentials; */

            myListService.UseDefaultCredentials = true;

            myListService.PreAuthenticate = true;

The full code listing for the XAML code-behind is as follows:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace WPFListApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string strCompanyName = "";
string strRegion = "";
string strSize = "";
string strSales = "";
string strListID = "";
string strViewID = "";

        public MainWindow()
{
InitializeComponent();
}

        private void btnExit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
strCompanyName = txtbxCompanyName.Text;
strRegion = txtbxRegion.Text;
strSize = txtbxSize.Text;
strSales = "$" + txtbxSales.Text;

            WPFListApp.MySPWebService.Lists myListService =
new MySPWebService.Lists();
//myListService.Credentials =
//System.Net.CredentialCache.DefaultCredentials;

            myListService.UseDefaultCredentials = true;
myListService.PreAuthenticate = true;

            myListService.Url = "https://intranet.contoso.com/_vti_bin/Lists.asmx";
            XmlNode myListView = myListService.GetListAndView("Customers", "");
strListID = myListView.ChildNodes[0].Attributes["Name"].Value;
strViewID = myListView.ChildNodes[1].Attributes["Name"].Value;
XmlDocument myListDoc = new XmlDocument();
XmlElement batchXML = myListDoc.CreateElement("Batch");
batchXML.InnerXml = "<Method ID = '1' Cmd='New'><Field Name='Title'>" +
strCompanyName + "</Field><Field Name='Region'>" + strRegion +
"</Field><Field Name='Size'>" + strSize +
"</Field><Field Name='Sales'>" + strSales +
"</Field>" + "</Method>";
XmlNode myListReturn = myListService.
UpdateListItems(strListID, batchXML);
MessageBox.Show("SharePoint List was updated!");
}

        private void btnClear_Click(object sender, RoutedEventArgs e)
{
txtbxCompanyName.Text = "";
txtbxRegion.Text = "";
txtbxSales.Text = "";
txtbxSize.Text = "";
}
}
}

I’ve added the full code sample here: https://cid-40a717fc7fcd7e40.office.live.com/browse.aspx/Beginning%5E_SP%5E_Supplements. Note that I will post any supplements, errata or additional code to this blog and add to the aforementioned folder.

Thanks to Jason for pointing this out!

Steve