Export (0) Print
Expand All

Lists.AddAttachment method

Adds an attachment to the specified list item in the specified list.

Namespace:  WebSvcLists
Assembly:  STSSOAP (in STSSOAP.dll)

[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
Type: System.String

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

listItemID
Type: System.String

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
Type: System.String

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

attachment
Type: []

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

Return value

Type: System.String
A string that contains the URL for the attachment, which can subsequently be used to reference the attachment.

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.

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);
}
Show:
© 2015 Microsoft