示例#1
0
        public int UploadToCrm(Stream fileStream, string filename, string contentType,
                               CrmContactEntity entity)
        {
            var request = new RestRequest("crm/{entityType}/{entityId}/files/upload.json", Method.POST);

            request.AddUrlSegment("entityType", entity.EntityTypeName)
            .AddUrlSegment("entityId", entity.Id.ToString())
            .AddParameter("storeOriginalFileFlag", false);

            request.AddFile(filename, fileStream.CopyTo, filename, fileStream.Length, contentType);

            var response = Execute(request);

            if (response.ResponseStatus != ResponseStatus.Completed ||
                (response.StatusCode != HttpStatusCode.Created &&
                 response.StatusCode != HttpStatusCode.OK))
            {
                throw new ApiHelperException("Upload file to crm failed.", response.StatusCode, response.Content);
            }

            var json = JObject.Parse(response.Content);

            var id = int.Parse(json["response"]["id"].ToString());

            return(id);
        }
示例#2
0
        public static int UploadToCrm(Stream fileStream, string filename, string contentType,
                                      CrmContactEntity entity)
        {
            var request = new RestRequest(string.Format("crm/{0}/{1}/files/upload.json", entity.Type.StringName(), entity.Id), Method.POST);

            request.AddParameter("storeOriginalFileFlag", false);

            var bytes = fileStream.GetCorrectBuffer();

            request.AddFile(filename, bytes, filename, contentType);

            var response = Execute(request);

            if (response.ResponseStatus != ResponseStatus.Completed ||
                (response.StatusCode != HttpStatusCode.Created &&
                 response.StatusCode != HttpStatusCode.OK))
            {
                throw new ApiHelperException("Upload file to crm failed.", response.StatusCode, response.Content);
            }

            var json = JObject.Parse(response.Content);

            var id = Int32.Parse(json["response"]["id"].ToString());

            return(id);
        }
示例#3
0
        public void AddToCrmHistory(MailMessage message, CrmContactEntity entity, IEnumerable <int> fileIds)
        {
            var request = new RestRequest("crm/history.json", Method.POST);

            var contentJson = string.Format("{{ message_id : {0} }}", message.Id);

            request.AddParameter("content", contentJson)
            .AddParameter("categoryId", MAIL_CRM_HISTORY_CATEGORY)
            .AddParameter("created", new ApiDateTime(message.Date));

            var crmEntityType = entity.EntityTypeName;

            if (crmEntityType == CrmContactEntity.CrmEntityTypeNames.contact)
            {
                request.AddParameter("contactId", entity.Id)
                .AddParameter("entityId", 0);
            }
            else
            {
                if (crmEntityType != CrmContactEntity.CrmEntityTypeNames.Case &&
                    crmEntityType != CrmContactEntity.CrmEntityTypeNames.opportunity)
                {
                    throw new ArgumentException(String.Format("Invalid crm entity type: {0}", crmEntityType));
                }

                request.AddParameter("contactId", 0)
                .AddParameter("entityId", entity.Id)
                .AddParameter("entityType", crmEntityType);
            }

            if (fileIds != null)
            {
                fileIds.ToList().ForEach(
                    id => request.AddParameter("fileId[]", id));
            }

            var response = Execute(request);

            if (response.ResponseStatus == ResponseStatus.Completed &&
                (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK))
            {
                return;
            }

            if (response.ErrorException is ApiHelperException)
            {
                throw response.ErrorException;
            }

            throw new ApiHelperException("Add message to crm history failed.", response.StatusCode, response.Content);
        }