DeleteAttachment Issue

Coordinator
Feb 12, 2012 at 2:10 PM
Edited Feb 12, 2012 at 2:10 PM

From DougHemminger on http://spservices.codeplex.com/wikipage?title=Lists

I am having a slight issue with "DeleteAttachment" and I was wondering if someone might be able to help. Specifically, I keep getting:

"Parameter Url is missing or invalid"

in the response xml with this code (Note that I obfuscated the url, but I verified that the url is working properly):

function DeleteAtt() {
  $().SPServices({
    operation: "DeleteAttachment",
    listName: "Promotion Nomination Employee Data",
    listItemID: $("input[id$=ListId]").val(),
    url: "http://blah/blah/blah/Attachments/17/ApprovalEmail.pdf",
    completefunc: function (xData, Status) {
      var out = $().SPServices.SPDebugXMLHttpResult({
        node: xData.responseXML
      });
      $("#WSOutput").html("").append(out);
    }
  });
}
Feb 12, 2012 at 2:34 PM
Marc,
According to your list name, it includes spaces. Is you URL escaped? (convert spaces to %20 or +)

_____
Paul

Sent from mobile device.

Coordinator
Feb 12, 2012 at 2:51 PM
Edited Feb 12, 2012 at 2:52 PM

Paul: It's Doug's list ;-), but spaces don't matter.

Doug:

Where are you seeing the "Parameter Url is missing or invalid" message? Is it in the returned XML? Can you post the full response?

M.

Feb 12, 2012 at 2:56 PM

Thanks for your help. Here is the full response:

 

#document
xml version="1.0" encoding="utf-8"
0 version 1.0
1 encoding utf-8
soap:Envelope
0 xmlns:soap http://schemas.xmlsoap.org/soap/envelope/
1 xmlns:xsi http://www.w3.org/2001/XMLSchema-instance
2 xmlns:xsd http://www.w3.org/2001/XMLSchema
soap:Body
soap:Fault
faultcode soap:Server
faultstring Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.
detail
errorstring Parameter Url is missing or invalid.
errorcode 0x82000001
Coordinator
Feb 12, 2012 at 3:17 PM

I've reproduced this. Working on it...

M.

Coordinator
Feb 12, 2012 at 3:32 PM

It looks like the docs *may* be wrong and the parameter should be "Url" rather than "url". However, when I switch to Url, I get this error:

<errorstring xmlns="http://schemas.microsoft.com/sharepoint/soap/">String reference not set to an instance of a String. Parameter name: source</errorstring>

Odd, since there's no "source" parameter. Something's not right.

M.

Feb 12, 2012 at 4:34 PM

Yeah, I tried Url when I first ran into the issue, suspecting that the docs were wrong, but ended up with an issue with that as well. I should have said that. Thanks so much for working on it Marc!

Feb 12, 2012 at 5:09 PM
Edited Feb 14, 2012 at 2:16 AM

Here's what I have (still working on the ui) in case you are interested in what this is for. The Upload button dynamically updates the files that appear right below the Supporting documentation header and attaches the document to the behind the scenes list item. Everything works except the delete button.

Photobucket

Feb 12, 2012 at 6:16 PM
Doug,
In the function you pasted above, the URL seems to be hardcoded. Is that for illustration only?
For listItemID you get the value from and input field... That same value needs to be used in the URL for the item that needs to be deleted (ex: ".../Attachments/<ID>/<file name>").
Might helpful to see the SOAP message for the request.
_____
Paul

Sent from mobile device.
Feb 12, 2012 at 6:31 PM
Edited Feb 12, 2012 at 6:34 PM

Yes, the url was hardcoded for illustration. Here is the code I am working with (note: the alert is just for debugging).

I get the link from the link that appears right next to the delete button. The spaces are escaped in the link with %20. I tried hardcoding it just to see if it made a difference if I used real spaces but it didn't seem to. I already verified that the list item id is valid and appearing correctly. When I have an invalid list item id it gives me a different error.

I'm not sure how to get the SOAP Request. I will try to figure it out, unless you have an easy way to do it.

function DeleteAttachment() {
    deleteAttachmentId = "#" + deleteAttachmentId;
    alert($(deleteAttachmentId).attr("href"));
    $().SPServices({
        operation: "DeleteAttachment",
        listName: "Promotion Nomination Employee Data",
        listItemID: $("input[id$=ListId]").val(),
        url: $(deleteAttachmentId).attr("href"),
        completefunc: function (xData, Status) {
            var out = $().SPServices.SPDebugXMLHttpResult({
                node: xData.responseXML
            });
            $("#WSOutput").html("").append("<b>This is the output from the GetList operation:</b>" + out);
            GetAttachments();
        }
    });
}

Feb 12, 2012 at 6:38 PM
Doug,
I'm going to try a quick demo later to see if I can recreate as well (I see Marc is also trying).

Re: getting the SOAP message

If you are using Firefox with firebug, the you can get it from the console when you enable the option to see ajax calls.

_____
Paul

Sent from mobile device.

Feb 12, 2012 at 7:32 PM

Doug,

I created a quick test case where I deleted a file attached to an existing List item and I was able to successful delete it.  I'm not sure of the issue you are having, but it worked for me... Here is the data I used:

jquery: 1.6.4

SPServices: 0.7.1 - ALPHA 10

SP: 2007

I added an attachment to a list item (ID = 1) and clicked OK on the EDIT page to save it...  I then did a 'GetAttachmentCollection' just to verify the URL of the attachments...

$().SPServices({
    operation:  "GetAttachmentCollection",
    listName:   "spservicetest",
    ID:         "1",
    async:      false,
    completefunc: function (xData, Status){
        
    }
});

Response :

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
    <GetAttachmentCollectionResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
        <GetAttachmentCollectionResult>
            <Attachments>
                <Attachment>http://...longURL.../Lists/spservicetest/Attachments/1/1111-Library-Test-Doc1.docx</Attachment>
            </Attachments>
        </GetAttachmentCollectionResult>
    </GetAttachmentCollectionResponse>
</soap:Body>
</soap:Envelope>

I then used the URL in the response above to delete the item...

$().SPServices({
    operation:  "DeleteAttachment",
    listName:   "spservicetest",
    listItemID: "1",
    url:        "http://...LongUrl.../Lists/spservicetest/Attachments/1/1111-Library-Test-Doc1.docx",
    async:      false,
    completefunc: function (xData, Status){
            
    }
});

Here is what the SOAP request looks like:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <DeleteAttachment xmlns="http://schemas.microsoft.com/sharepoint/soap/">
            <listName>spservicetest</listName>
            <listItemID>1</listItemID>
            <url>http://...longUrl.../Lists/spservicetest/Attachments/1/1111-Library-Test-Doc1.docx</url>
        </DeleteAttachment>
    </soap:Body>
</soap:Envelope>

And got back a successful response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <DeleteAttachmentResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"></DeleteAttachmentResponse>
    </soap:Body>
</soap:Envelope>

Sorry I could not help... The only thing I can think of is to check permissions and insure that the files are being uploaded correctly...  Doing the GetAttachmentCollection may help to insure yo have the right information...

Paul.

 

 

 


Coordinator
Feb 12, 2012 at 9:01 PM

After seeing Paul's success, I tried with a different attachment on a different item and was successful. Odd.

There must be something about the attachments that Doug and I were trying to delete that is different, but I can't spot anything.

M.

Feb 12, 2012 at 9:24 PM

I've been having a few issues with creating items within folders of my choice.  If I do this, the "Title" column has to be unique within this folder.  It's probably something due to an event receiver forcing the list to act like a Document Library.  So, long story short, the deletion may be affected with how the item was originally uploaded.  Based on Doug's example, it appears he's doing a custom upload and not using the OOTB parts for the uploading of attachments.

Feb 12, 2012 at 9:27 PM

Somehow it's the spaces. I deleted the site collection and redeployed my solution package with a new list name. The old list name had spaces in it. The new list name does not. Now, as long as the file name doesn't have spaces, it works. If the filename has spaces, I get the same error as before. I'm still trying to track down the issue with the spaces. Thanks so much for your help. The fact that it worked for you helped me to clarify what was different and try a few different things.

Feb 12, 2012 at 9:37 PM

By the way, I tried both %20 and just a plain space for the url and neither of them seem to work. I am going to try + next.

Feb 12, 2012 at 11:30 PM

Doug,
Glad to hear you are making progress. If get some time tomorrow, I'll try with a file that has spaces to see if I get an error.

----- Paul Sent from mobile
Feb 13, 2012 at 8:09 PM

Doug,

Did another quick test with a file that had spaces and was able to delete it with no errors... I have not tried with a List name that has spaces.

Paul.

Feb 14, 2012 at 12:50 AM

Paul, thanks for trying it. Did you use the same code as in your first example? One thing I am wondering is if this is a SharePoint issue with the version I am using. I am using SP 2007 SP2. I am going to try a little bit more tonight to see if I can get it working with spaces.

Feb 14, 2012 at 1:02 AM
Doug,
Yes. Same code as before.
I don't know what version of SP 2007 they are running here.

_____
Paul

Sent from mobile device.

Feb 14, 2012 at 2:14 AM
Edited Feb 14, 2012 at 2:17 AM

I figured out my issue. Sigh...I hate when I don't figure stuff like this out on my own. Thanks so much for your help. It really helped me figure out and isolate what the problem was. Full documentation of the problem and resolution is below:

I have an asp.net FileUpload control with an asp.net upload button. It looks like this:

Photobucket

The html for 1 is below:

<asp:FileUpload Height="25px" Width="500px" id="FileUploadControl" runat="server" />

The html for 2 is below:

<asp:Button Height="25px" Width="90px" runat="server" id="UploadButton" OnClientClick="UpdateDataToList();" OnClick="UploadButton_Click" text="Upload"/>

The upload button calls a client side function that saves the other data on the form to a List. It also calls a server side method that does some stuff and then attaches the file to the list item. The page then updates and looks like this:

Photobucket
Here is the html for the delete button:
<button OnClick="DeleteAttachment();" class="deleteButton">Delete</button>
 
Here is the html for the "File With Spaces.xls"
<a id="attachment01" href=http://[path]/File%20With%20Spaces.xls>"File With Spaces.xmlx"</a>
 
Note that the href and name of the file are put into the <a> tag with jQuery. It's part of a function that populates that little section (between "Supporting Documentation" and the file upload control) with all of the attachments. The function looks like this:
 
function GetAttachments() {
    $().SPServices({
        operation: "GetAttachmentCollection",
        listName: "ListName",
        ID: $("input[id$=ListId]").val(),
        completefunc: function (xData, Status) {
            $("#FileLinkPlaceholder").show();
            $("#FileLinkPlaceholder").html("");
            var i = 0;
            $(xData.responseXML).find("Attachment").each(function () {
                var attachmentLink = $(this).text();
                deleteAttachmentId = "attachment" + i;
                $("#FileLinkPlaceholder").append("<button OnClick=\"DeleteAttachment();\" class=\"deleteButton\">Delete</button><a id=\"" + deleteAttachmentId + "\" href=\"" + attachmentLink + "\">" + attachmentLink.substring(attachmentLink.lastIndexOf("/") + 1) + "</a><br/>");
                i = i + 1;
            });
        }
    });
}
Then when someone presses the delete button, it calls the Delete Attachment function, which looks like this:
function DeleteAttachment() {
    deleteAttachmentId = "#" + deleteAttachmentId;
    $().SPServices({
        operation: "DeleteAttachment",
        listName: "PromotionNominationEmployeeData",
        listItemID: $("input[id$=ListId]").val(),
        url: $(deleteAttachmentId).attr("href").replace(/%20/g, " "),
        completefunc: function (xData, Status) {
            GetAttachments();
        }
    });
}

The issue was that the href in the <a> tag was escaping the spaces with %20. And either SPServices or the SP Web service didn't like that when it was being passed through. So, I converted the %20 to spaces with .replace(/%20/g, " ") and everything worked.

I hope this helps someone else out in the future.

Feb 14, 2012 at 2:18 AM
Doug,
Glad that I could help you in figuring this out.


_____
Paul

Sent from mobile device.

Coordinator
Feb 14, 2012 at 12:55 PM

I think the %20 was my problem, too. Glad you guys got it solved! (And sorry I didn't have the answer.)

M.