共用方式為


Lists.AddAttachment method

在指定的清單中,會新增至指定的清單項目的附件。

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

Syntax

'宣告
<SoapDocumentMethodAttribute("https://schemas.microsoft.com/sharepoint/soap/AddAttachment", RequestNamespace := "https://schemas.microsoft.com/sharepoint/soap/",  _
    ResponseNamespace := "https://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
'用途
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)
[SoapDocumentMethodAttribute("https://schemas.microsoft.com/sharepoint/soap/AddAttachment", RequestNamespace = "https://schemas.microsoft.com/sharepoint/soap/", 
    ResponseNamespace = "https://schemas.microsoft.com/sharepoint/soap/", 
    Use = SoapBindingUse.Literal, ParameterStyle = SoapParameterStyle.Wrapped)]
public string AddAttachment(
    string listName,
    string listItemID,
    string fileName,
    byte[] attachment
)

參數

  • listName
    Type: System.String

    字串,包含標題或清單的 GUID。

  • listItemID
    Type: System.String

    內含附件新增至其中的項目識別碼的字串。此值並未對應至清單項目集合內的項目索引。

  • fileName
    Type: System.String

    包含要新增為附件的檔案名稱的字串。

  • attachment
    Type: []

    位元組陣列,包含要使用 base 64 編碼附加的檔案。

傳回值

Type: System.String
內含附件,隨後可以用來參照附件的 URL 的字串。

Examples

下列程式碼範例會將指定的清單項目以附件形式新增本機檔案。範例會使用System.IO.FileStream物件的來源檔案讀入傳送AddAttachment方法的參數為位元組陣列。

本範例會要求using (Visual C#) 或System.IO命名空間所包含的Imports (Visual Basic 中) 指示詞。此範例也會假設 Windows 應用程式的表單中的文字方塊存在。

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
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);
}

請參閱

參照

Lists class

Lists members

WebSvcLists namespace