Lists.AddAttachment Method (Lists)
Adds an attachment to the specified list item in the specified list.

Web Service: ListsWeb Reference: http://<Site>/_vti_bin/Lists.asmx
Syntax

Visual Basic (Declaration)
<SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/AddAttachment", RequestNamespace:="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace:="http://schemas.microsoft.com/sharepoint/soap/", Use:=SoapBindingUse.Literal, ParameterStyle:=SoapParameterStyle.Wrapped)> _
Public Function AddAttachment ( _
    listName As String, _
    listItemID As String, _
    fileName As String, _
    attachment As Byte() _
) As String
Visual Basic (Usage)
Dim instance As Lists
Dim listName As String
Dim listItemID As String
Dim fileName As String
Dim attachment As Byte()
Dim returnValue As String

returnValue = instance.AddAttachment(listName, listItemID, fileName, attachment)
C#
[SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/AddAttachment", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] 
public string AddAttachment (
    string listName,
    string listItemID,
    string fileName,
    byte[] attachment
)

Parameters

listName

A string that contains either the title or the GUID for the list.

listItemID

A string that contains the ID of the item to which attachments are added. This value does not correspond to the index of the item within the collection of list items.

fileName

A string that contains the name of the file to add as an attachment.

attachment

A byte array that contains the file to attach by using base-64 encoding.

Return Value

A string that contains the URL for the attachment, which can subsequently be used to reference the attachment.
Example

The following code example adds a local file as an attachment to a specified list item. The example uses a System.IO.FileStream object to read the source file into a byte array that is passed as a parameter of the AddAttachment method.

This example requires that a using (Visual C#) or Imports (Visual Basic) directive be included for the System.IO namespace. The example also assumes the existence of a text box in the form of the Windows application.

Visual Basic
Dim srcUrl As String = textBox1.Text

If Not File.Exists(srcUrl) Then

    Throw New ArgumentException(String.Format("{0} does not exist", 
        srcUrl), "srcUrl")

End If

Dim fStream As FileStream = File.OpenRead(srcUrl)
Dim fileName As String = fStream.Name.Substring(3)
Dim contents(fStream.Length) As Byte
fStream.Read(contents, 0, CInt(fStream.Length))

fStream.Close()

Dim listService As New Web_Reference_Folder.Lists()
listService.Credentials = System.Net.CredentialCache.DefaultCredentials

Try

    Dim addAttach As String = listService.AddAttachment("List_Name", 
        "3", fileName, contents)

    MessageBox.Show(addAttach)

    Catch ex As System.Web.Services.Protocols.SoapException

        MessageBox.Show("Message:" + ControlChars.Lf + ex.Message + 
                ControlChars.Lf + _
            "Detail:" + ControlChars.Lf + ex.Detail.InnerText + 
                ControlChars.Lf + _
            "StackTrace:" + ControlChars.Lf + ex.StackTrace)

End Try
C#
string srcUrl = textBox1.Text;

if (! File.Exists(srcUrl))
{
    throw new ArgumentException(String.Format("{0} does not exist", 
        srcUrl), "srcUrl");
}

FileStream fStream = File.OpenRead(srcUrl);
string fileName = fStream.Name.Substring(3);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();

Web_Reference_Folder.Lists listService = 
    new Web_Reference_Folder.Lists();
listService.Credentials= System.Net.CredentialCache.DefaultCredentials;

try
{
    string addAttach = listService.AddAttachment("List_Name", "3", 
        fileName, contents);
    MessageBox.Show(addAttach);
}

catch (System.Web.Services.Protocols.SoapException ex)
{
    MessageBox.Show("Message:\n" + ex.Message + "\nDetail:\n" + 
        ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace);
}
See Also

Tags :


Community Content

manuelityo
SoapServerException

Hello:

I'm trying to upload a file in a Document Library using the List.asmx Web Services of SharePoint and the AddAttachment method, but is almost impossible to do that because I simply can´t find the correct value of listItemID parameter.

I obtain an "Microsoft.SharePoint.SoapServer.SoapServerException" type error, "The value is not in the expected interval" ("Value does not fall within the expected range", sorry for the translation).

I would like to know how to upload a document with this metod. If somebody know how to do that will be correct to put it here.

Thanks

Tags :

Kristian Ask
SoapServerException

Hi,

I also get the exact same thing.

Kristian

Tags :

Firas Ammouri
This approach worked for me.

string addAttach = listService.AddAttachment(ListGuid, "62", fileName, contents);

In this case, I used a literal string for the ID field value for demo purposes. In my WSS 3.0 list, there exists a field called ID of type "Counter". It has the "Primary Key" column set to true. I think it gets generated when you create the list.

The "ListGuid" looks something like this: A17L9B44-8435-2C13-DA46-B2BD87036347X

The file name is the contents of the TextBox field and the contents.

The contents is an array of bytes and was generated using the code in the example.

I hope this helps.

Regards,
Jason.

Tags :

George Takei
How to get ListItemId

Hi Guys,

You have to note that you can get the ListItemId once you add a new item using the “UpdateListItems” web method. The xml that has to return after adding new item is something like:

<Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<Result ID="1,New">
<ErrorCode>0x00000000</ErrorCode>
<ID />
<z:row
ows_ID="26"
ows_ContentTypeId="0x0100611F3EE398C7BC4FA2D9C79647ABE9E5"
ows_ContentType="Item"
ows_Title="New Item Title"
ows_Modified="2008-04-10 14:00:21"
ows_Created="2008-04-10 14:00:21"
ows_Author="153;#Domain\userName"
ows_Editor="153;#Domain\UserName"
ows_owshiddenversion="1"
ows_WorkflowVersion="1"
ows__UIVersion="512"
ows__UIVersionString="1.0"
ows_Attachments="0"
ows__ModerationStatus="0"
ows_LinkTitleNoMenu="Title Name"
ows_LinkTitle="Title Name"
ows_SelectTitle="26"
ows_Order="2600.00000000000"
ows_GUID="{0411F72F-2760-43C2-81AE-2E6A5979F7B5}"
ows_FileRef="26;#SomeList/Lists/ListName/26_.000"
ows_FileDirRef="26;#SomeList/Lists/ListName"
ows_Last_x0020_Modified="26;#2008-04-10 14:00:21"
ows_Created_x0020_Date="26;#2008-04-10 14:00:21"
ows_FSObjType="26;#0"
ows_PermMask="0x7fffffffffffffff"
ows_FileLeafRef="26;#26_.000"
ows_UniqueId="26;#{BBECF870-2262-4622-8B6D-FE5AE719B4A4}"
ows_ProgId="26;#"
ows_ScopeId="26;#{B1D2327E-D66E-4248-9120-F781C1BFDDB1}"
ows__EditMenuTableStart="26_.000"
ows__EditMenuTableEnd="26"
ows_LinkFilenameNoMenu="26_.000"
ows_LinkFilename="26_.000"
ows_ServerUrl="/SomeList/Lists/ListName/26_.000"
ows_EncodedAbsUrl="http://portal/SomeList/Lists/ListName /26_.000"
ows_BaseName="26_"
ows_MetaInfo="26;#"
ows__Level="1"
ows__IsCurrentVersion="1"
xmlns:z="#RowsetSchema" />
</Result>
</Results>

The ListITemId is: ows_ID that is 26.

Anyhow, if you want to get the ID for already created List Item, then you have to get the meta data first. To get the list meta data you have to use: list.GetListCollection()

Regards,

Firas.


-----------------------------------------------------------------------------------------------
Does anyone know what Firas is trying to say? How are the attributes of a list supposed to help find the 'ID' of an item?

"A string that contains the ID of the item to which attachments are added. This value does not correspond to the index of the item within the collection of list items."

As fun as vague hints are, a description that explains what listItemID IS rather than what it is not would be more helpful in figuring out how to get it. Or maybe a few more sentences about what it isnt because I'm having a lot of trouble figuring out which value I'm using isnt within the expected range or what range is expected

Tags :

Page view tracker