Andrew
Andrew

Reputation: 2335

Adding items to a SharePoint list using UpdateList

I've come across a SharePoint problem that I hope someone can help with. Basically, when I am trying to add a column called "MigratedChemist" to a list called "WorkCards" (pListName in the method parameters). No matter what I try I am getting a FaultException error raised when calling UpdateList. I am connecting using the SharePoint web service. I have confirmed the following:

  1. The column doesn't already exist and I do have permissions to create it in SharePoint
  2. The connection to SharePoint is established corectly to /_vti_bin/lists.asmx
  3. The list name is correct as I have another method which returns items from the list and that works perfectly.
  4. The xVersion and xId values are set correctly when the program runs and passed as parameters - as far as I am concerned I should just be able to pass the list name, as opposed to the GUID, but neither method works.

My code is as follows:

public static bool AddColumnToList(string pUri, string pListName, string pViewName, string pMaxRecords)
    {

        string version = string.Empty;
        XAttribute xId = null;
        XAttribute xVersion = null;

        try
        {
            XElement listDetails = client.GetList(pListName);
            xVersion = listDetails.Attribute("Version");
            xId = listDetails.Attribute("ID");
        }
        catch { throw; }

        XElement ndNewFields = new XElement ("Fields", "");
        string newXml = "<Method ID='1' Cmd='New'><Field Name='MigratedChemist' Type='Text' DisplayName='MigratedChemist' /></Method></Fields>";

        ndNewFields.Add(newXml);

        XElement result;

        try
        {
            result = client.UpdateList(xId.Value, null, ndNewFields, null, null, xVersion.Value);
        }
        catch (FaultException fe)
        {
        }


        return true;
    }

In addition to this does anyone know how to get any decent information from FaultRequest? At the moment I get the following error message, which is of no use and there appears to be no extra detail. I have tried, as some have suggested removing the error handling and letting the program halt, but that doesn't give me any extra information either.

{"Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown."}

Upvotes: 1

Views: 1456

Answers (1)

Andrew
Andrew

Reputation: 2335

For future reference I have solved both the problem of the SharePoint update failing AND the FaultException problem, so am including it here for posterity:

Problem 1 - Problem with UpdateList

This problem was caused because my XML was malformed. When I called ndNewFields.Add(newXml) then XML that was being returned was replacing < and > than with control characters, as shown below (I have added an extra space because this editor converts them automatically.

<Method ID="1" Cmd="New"&gt ;&lt ;Field Name="MigratedChemist"&gt ;&lt ;/Field&gt ;&lt ;/Method&gt ;

Now, I did notice this pretty early on but wasn't sure whether it would cause a problem or not. However using the XElement.Parse command I was able to remove these characters and this resolved the problem:

ndNewFields.Add(XElement.Parse (newXml));

Problem 2 - Problem with the SharePoint error coming back as FaultException

This has been bugging for me ages so I am glad I have finally solved it. I can't take credit for it, as I took it from another page, but the following code was how I got the details.

catch (FaultException fe)
        {
            MessageFault msgFault = fe.CreateMessageFault();
            XmlElement elm = msgFault.GetDetail<XmlElement>();
}

Hopefully this will save someone some frustration in the future!

Andrew

Upvotes: 1

Related Questions