//CreateWithAttachment(ServiceURL,ListName,"158","");
const string ListName = "NAME OF YOUR LIST";
const string ServiceURL = "http://mysite/sites/sitename/_vti_bin/Lists.asmx";
class CreateState
{
public bool hasErrors;
public string returnMessage;
public string fileName;
}
//webservicename- name of your web service
CreateState CreateWithAttachment(string serviceurl, string listname, string itemid, string filename)
{
CreateState cs = new CreateState();
cs.fileName = Path.GetFileName(filename);
if (File.Exists(filename))
{
byte[] contents = System.IO.File.ReadAllBytes(filename);
webservicename.Lists listService = new webservicename.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = serviceurl;
try
{
string addAttach = listService.AddAttachment(listname, itemid,
cs.fileName, contents);
cs.hasErrors = false;
cs.returnMessage = addAttach;
return cs;
}
catch (System.Web.Services.Protocols.SoapException ex)
{
cs.hasErrors = true;
cs.returnMessage = "ERROR: \n" +
ex.Detail.InnerText ;
return cs;
}
}
else
{
cs.hasErrors = true;
cs.returnMessage = "ERROR: file not found";
return cs;
}
}
Комментарии