using System using System Data using System Web using System Collectio

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
//using System.ComponentModel;
using UpdateContactPicture.CRM;
namespace UpdateContactPicture
{
[WebService(Namespace = "http://tempuri.org/")]
public class UpdatePicture : System.Web.Services.WebService
{
private const string CONTACT_PICTURE = "Contact Picture";
public string UploadContactPicture(string contactId, string name , Int32 fileLen )//, byte[] uploadFile, string fileExt)
{
//contactId = "C082C314-7DF3-DD11-83FF-0003FFFB7389";
contactId = "30B02DC9-EEB1-DB11-BB12-0003FFFB7389";
byte[] uploadFile = new byte[] {1,1,1,1,1,1,1};
string fileExt = ".jpeg";
// a0bf2d28-ce5b-de11-a38b-0003ffe07389
// 00886f19-cf5b-de11-a38b-0003ffe07389
string status = "OK";
annotation ContactPicture = null;
contactId.Remove(1, 1);
contactId.Remove(35, 1);
fileExt = fileExt.Remove(0,1);
try
{
CrmService crmService = new CrmService();
#if DEBUG
crmService.Credentials = new System.Net.NetworkCredential("administrator","123456","sitcrm4");
#else
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
#endif
ColumnSet cs = new ColumnSet();
cs.Attributes = new string[] { "contactid", "new_pictureid" };
contact cont = (contact)crmService.Retrieve(EntityName.contact.ToString(), new Guid(contactId), cs);
if (cont.contactid != null)
{
if (cont.new_pictureid == null || cont.new_pictureid == String.Empty)
{
ContactPicture = new annotation();
ContactPicture.objectid = new Lookup();
ContactPicture.objectid.Value = cont.contactid.Value;
ContactPicture.objectid.type = EntityName.contact.ToString();
ContactPicture.isdocument = new CrmBoolean();
ContactPicture.isdocument.Value = true;
ContactPicture.objecttypecode = new EntityNameReference();
ContactPicture.objecttypecode.Value = EntityName.contact.ToString();
ContactPicture.subject = CONTACT_PICTURE;
}
else
{
try
{
ContactPicture = (annotation)crmService.Retrieve(EntityName.annotation.ToString(), new Guid(cont.new_pictureid), new AllColumns());
}
catch (Exception ex)
{
ContactPicture = new annotation();
ContactPicture.objectid = new Lookup();
ContactPicture.objectid.Value = cont.contactid.Value;
ContactPicture.objectid.type = EntityName.contact.ToString();
ContactPicture.isdocument = new CrmBoolean();
ContactPicture.isdocument.Value = true;
ContactPicture.objecttypecode = new EntityNameReference();
ContactPicture.objecttypecode.Value = EntityName.contact.ToString();
ContactPicture.subject = CONTACT_PICTURE;
}
}
//System.Web.UI.WebControls.FileUpload upload = new System.Web.UI.WebControls.FileUpload();
//upload = (System.Web.UI.WebControls.FileUpload)uploadFile;
ContactPicture.filesize = new CrmNumber();
ContactPicture.filename = name;
ContactPicture.filesize.Value = fileLen;
UploadFromBase64DataAnnotationRequest upload = new UploadFromBase64DataAnnotationRequest();
if (ContactPicture.annotationid != null)
{
upload.AnnotationId = ContactPicture.annotationid.Value;
upload.FileName = name;
//upload.MimeType = "application/pdf";
upload.MimeType = "image/"+ fileExt;
upload.Base64Data = System.Convert.ToBase64String(uploadFile);
UploadFromBase64DataAnnotationResponse uploaded = (UploadFromBase64DataAnnotationResponse)crmService.Execute(upload);
TargetUpdateAnnotation trgAnnotation = new TargetUpdateAnnotation();
trgAnnotation.Annotation = ContactPicture;
UpdateRequest updAnnotation = new UpdateRequest();
updAnnotation.Target = trgAnnotation;
UpdateResponse updrAnnotation = (UpdateResponse)crmService.Execute(updAnnotation);
cont.new_pictureid = ContactPicture.annotationid.Value.ToString();
}
else
{
TargetCreateAnnotation trgAnnotation = new TargetCreateAnnotation();
trgAnnotation.Annotation = ContactPicture;
CreateRequest crAnnotation = new CreateRequest();
crAnnotation.Target = trgAnnotation;
CreateResponse crrAnnotation = (CreateResponse)crmService.Execute(crAnnotation);
cont.new_pictureid = crrAnnotation.id.ToString();
upload.AnnotationId = crrAnnotation.id;
upload.FileName = name;
upload.MimeType = "image/"+ fileExt;
upload.Base64Data = System.Convert.ToBase64String(uploadFile);
UploadFromBase64DataAnnotationResponse uploaded = (UploadFromBase64DataAnnotationResponse)crmService.Execute(upload);
}
TargetUpdateContact trgContact = new TargetUpdateContact();
trgContact.Contact = cont;
UpdateRequest updReq = new UpdateRequest();
updReq.Target = trgContact;
UpdateResponse updResp = (UpdateResponse)crmService.Execute(updReq);
}
}
catch (SoapException ex)
{
status = ex.Message;
}
catch (Exception ex)
{
status = ex.Message;
}
return status;
}
}
}