Here is an example how to create Activity and upload attachment to it:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Services.Client;
using System.Net;
using ODataProxyClient.ODataProxyClient;//Reference to EntityDataService.svc proxy
namespace ODataProxyClient
{
class Program
{
//Specify your website Uri here
private const string baseUri = "https://yoursite.bpmonline.com";
private static Uri serverUri = new Uri(baseUri + @"/0/ServiceModel/EntityDataService.svc/");
static void Main(string[] args)
{
CreateActivityWithAttachment();
}
static void OnSendingRequestCookie(object sender, SendingRequestEventArgs e)
{
Authentification.TryLogin("Supervisor", "Supervisor", baseUri);
var req = e.Request as HttpWebRequest;
req.CookieContainer = Authentification.AuthCookie;
e.Request = req;
}
public static void CreateActivityWithAttachment()
{
Guid activityId = Guid.NewGuid();
//Create application context
var context = new BPMonline(serverUri);
DataServiceRequestArgs _args = new DataServiceRequestArgs();
//Define content type
_args.ContentType = "application/octet-stream";
//Read file from file system
byte[] bytes = System.IO.File.ReadAllBytes(@"C:\Activity.csv");
using (var fs = new MemoryStream(bytes))
{
//Create new activity. Remember to specify all required fields
var activity = new Activity()
{
Id = activityId,
Title = "Test activity with attachment",
TypeId = new Guid("FBE0ACDC-CFC0-DF11-B00F-001D60E938C6"),
StatusId = new Guid("384D4B84-58E6-DF11-971B-001D60E938C6"),
AuthorId = new Guid("410006E1-CA4E-4502-A9EC-E54D922D2C00"),
OwnerId = new Guid("410006E1-CA4E-4502-A9EC-E54D922D2C00"),
StartDate = DateTime.Now,
DueDate = DateTime.Now,
ActivityCategoryId = new Guid("F51C4643-58E6-DF11-971B-001D60E938C6"),
PriorityId = new Guid("AB96FA02-7FE6-DF11-971B-001D60E938C6")
};
//Create new activity attachment
//Remember to specify filename with extension
ActivityFile NewFile = new ActivityFile()
{
Id = Guid.NewGuid(),
Name = "Activity.csv",//File name with extension
Size = (int)fs.Length,
TypeId = new Guid("529BC2F8-0EE0-DF11-971B-001D60E938C6"),//Attachment type - File
Uploaded = true
};
//Define method for authentication
context.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequestCookie);
//Add created activity to collection
context.AddToActivityCollection(activity);
//Add new activity file to collection
context.AddToActivityFileCollection(NewFile);
//Link file with activity
context.SetLink(NewFile, "Activity", activity);
//Save changes
DataServiceResponse responces = context.SaveChanges();
//Write file stream to activity file
context.SetSaveStream(NewFile, "Data", fs, true, _args);
//Save changes
context.SaveChanges();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
// String of address bpm’online OData servise.
private const string serverUri = "https://0424747-sales-enterprise-demo.bpmonline.com/0/ServiceModel/EntityDataService.svc/";
private const string authServiceUtri = "https://0424747-sales-enterprise-demo.bpmonline.com/ServiceModel/AuthService.svc/Login";
// Links to XML name spaces.
private static readonly XNamespace ds = "http://schemas.microsoft.com/ado/2007/08/dataservices";
private static readonly XNamespace dsmd = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
private static readonly XNamespace atom = "http://www.w3.org/2005/Atom";
private static readonly Guid FileTypeId = new Guid("529BC2F8-0EE0-DF11-971B-001D60E938C6");
static void Main(string[] args)
{
var file = ReadFile("z:/2SupportLine/chrome.png");
var id = CreateBpmEntityByOdataHttpExample();
TransferImage(id, "Data", file);
}
public static Guid CreateBpmEntityByOdataHttpExample()
{
Guid id = Guid.NewGuid();
// Creating a xml message containing data on the created object.
var content = new XElement(dsmd + "properties",
new XElement(ds + "ContactId", new Guid("410006e1-ca4e-4502-a9ec-e54d922d2c00")),//supervisor contact Id
new XElement(ds + "Id", id.ToString()),
new XElement(ds + "TypeId", FileTypeId.ToString()),
new XElement(ds + "Name", "test1.png")
);
var entry = new XElement(atom + "entry",
new XElement(atom + "content",
new XAttribute("type", "application/xml"), content));
Console.WriteLine(entry.ToString());
// Creating a request to the service which will add a new object to the contacts collection.
var request = (HttpWebRequest)HttpWebRequest.Create(serverUri + "ContactFileCollection/");
request.Credentials = new NetworkCredential("Supervisor", "Supervisor");
request.Method = "POST";
request.Accept = "application/atom+xml";
request.ContentType = "application/atom+xml;type=entry";
// Recording the xml message to the request stream.
using (var writer = XmlWriter.Create(request.GetRequestStream()))
{
entry.WriteTo(writer);
}
// Receiving a response from the service regarding the operation implementation result.
using (WebResponse response = request.GetResponse())
{
if (((HttpWebResponse)response).StatusCode == HttpStatusCode.Created)
{
// Processing the operation implementation result.
}
}
return id;
}
public static void TransferImage(Guid fileRecordId, string columnName, byte[] file)
{
// Creating a request to the service which will add a new object to the contacts collection.
var request = (HttpWebRequest)HttpWebRequest.Create(serverUri + "ContactFileCollection(guid'" + fileRecordId.ToString() + "')/" + columnName);
request.Credentials = new NetworkCredential("Supervisor", "Supervisor");
request.Method = "PUT";
request.Accept = "application/octet-stream,application/json;odata=verbose";
request.ContentType = "multipart/form-data;boundary=+++++";
// Recording the xml message to the request stream.
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(file, 0, file.Length);
}
// Receiving a response from the service regarding the operation implementation result.
using (WebResponse response = request.GetResponse())
{
if (((HttpWebResponse)response).StatusCode == HttpStatusCode.Created)
{
// Processing the operation implementation result.
}
}
}
public static byte[] ReadFile(string filePath)
{
byte[] imageArray = System.IO.File.ReadAllBytes(filePath);
return imageArray;
}
}
}
I know this is a bit of an old post but I'm currently trying to implement your HttpWebRequest example. Do you know if this is still the correct way to implement the upload or has something changed? When I run this, I get a StatusCode of "NoContent" (not "Created") - with no error message or anything. Any help would be appreciated.
It is the TransferImage portion which is causing a problem. I have modified your code a bit to fit my use case but the end result is yes I get a new attachment record, but I can't click to actually download the file. I basically get a 404. Yes I have confirmed I am reading the file correctly and getting the byte array into the 'file' variable.
The fact that you receive the 404 exception means that you didn't load the attachment in the "TransferImage" method.
Please try to run my code instead the one that you modified. Please check it if works without the moifications. Please sent me the code that you ran. Additionally, please catch the request that you send in the TransferImage method with fiddler and let me check it.
I went back to your example and it worked.. So I went back to my code and realized that I hadn't assigned the TypeId field as you do in your example. I didn't know that was a required field - but apparently it is. My code is now working - thanks for your help!
This is an old post, but I am testing your code to create attachments in 7.13. I am using your sample code as-is with the exception of changing URLs etc.
The ContactFile record is created file in the CreateBpmEnityByODataHttpExample method, however, when the TransferImage method runs I get a 405 "method not allowed" error. I've tried PATCH and MERGE instead of PUT with the same results.
Do you happen to know if this should still work in 7.13? Maybe I am missing something?
Unfortunately, there is no such possibility on current system version, Please feel free to use basic functionality for working with files. Take a look at FileServices and FileRepository schemas in the system configuration. There you can find methods for upload/download of file and use it in your development.