示例#1
0
        public ArtefactFileResponse ArtefactFileUpload(ArtefactFile artefactFile)
        {
            // Create listitem, and set metadata
            ItemResponse ret = ArtefactService.SetArtefactFile(artefactFile, AppSettings.ArtefactFileContentType);
            if (ret.Response.ReturnCode != 200)
                return new ArtefactFileResponse(ret.Response);

            artefactFile.Id = ret.Id;

            // Update listitem with unique fileid
            ArtefactFile updateFile = new ArtefactFile();
            updateFile.Id = ret.Id;
            updateFile.ArtefactId = string.Format("{0}-{1}", Guid.NewGuid().ToString(), ret.Id);
            ret = ArtefactService.SetArtefactFile(updateFile);
            if (ret.Response.ReturnCode != 200)
                return new ArtefactFileResponse(ret.Response);;

            // Update listitem with attachment
            Response resp = ArtefactService.SetArtefactAttachment(artefactFile);
            if (resp.ReturnCode != 200)
                return new ArtefactFileResponse(resp);

            // Return created item details
            ArtefactFileResponse response = new ArtefactFileResponse(resp);
            response.Response.ReturnCode = 201;
            response.Id = updateFile.Id;
            response.ArtefactId = updateFile.ArtefactId;
            return response;
        }
示例#2
0
        private void CreateArtefact()
        {
            ArtefactFile artefact = new ArtefactFile();

            string filePath = @"c:\temp\superman_dc_universe_online-1920x1080.jpg";
            byte[] binary = System.IO.File.ReadAllBytes(filePath);
            string fname = System.IO.Path.GetFileName(filePath);

            artefact.Title = fname;
            artefact.FileName = fname;
            artefact.Firstname = "superman";
            artefact.Surname = "wonder woman";
            artefact.FileBase64 = Convert.ToBase64String(binary);

            using (var client = new WebClient())
            {
                client.UseDefaultCredentials = true;
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                var json = JsonConvert.SerializeObject(artefact);
                var result = client.UploadString(@"http://sp2013demo/_vti_bin/DM/DMService.svc/ArtefactFileUpload", json);
                if (!string.IsNullOrEmpty(result))
                {
                    Response.Write(result);
                    ArtefactFileResponse t = JsonConvert.DeserializeObject<ArtefactFileResponse>(result);

                }
            }
        }
        public static ArtefactFile CreateArtefactFile(JObject json)
        {
            ArtefactFile response = new ArtefactFile();
            response.Title = json.GetSafeValue<string>("Title");
            //response.SourceId = json.GetSafeValue<string>("SourceId");
            response.Id = json.GetSafeValue<int>("ID");
            response.ArtefactId = json.GetSafeValue<string>("ArtefactId");

            response.Surname = json.GetSafeValue<string>("Surname");
            response.Firstname = json.GetSafeValue<string>("Firstname");
            response.IdNumber = json.GetSafeValue<string>("IdNumber");
            //response.Status = json.GetSafeValue<int>("Status");
            //response.FileId = json.GetSafeValue<string>("FileId");
            response.FileName = json.GetSafeValue<string>("ArtefactFileName");
            //this.CreatedDateDA = json.GetSafeValue<DateTime>("CreatedDateDA");
            //this.ModifiedDateDA = json.GetSafeValue<DateTime>("ModifiedDateDA");
            response.CreatedByDA = json.GetSafeValue<string>("CreatedByDA");
            response.ModifiedByDA = json.GetSafeValue<string>("ModifiedByDA");

            return response;
        }
示例#4
0
        /// <summary>
        /// Returns the byte stream of a listitem attachment
        /// </summary>
        /// <param name="artefact"></param>
        /// <returns></returns>
        public static ItemResponse GetArtefactAttachment(ArtefactFile artefact)
        {
            ItemResponse response = new ItemResponse();
            byte[] bytes = new byte[256];

            // Set REST url
            var url = string.Format("{0}/Lists/Artefacts/Attachments/{1}/{2}", AppSettings.Portal, artefact.Id, artefact.FileName);

            HttpWebRequest endpointRequest = (HttpWebRequest)WebRequest.Create(url);
            endpointRequest.Timeout = 1000000;
            endpointRequest.AllowWriteStreamBuffering = false;

            // Set credentials
            //endpointRequest.Credentials = Credentials.GetSharePointCredential();
            var ret = Credentials.SetSharePointCredential(endpointRequest);
            if (ret.ReturnCode != 200)
            {
                response.Response = ret;
                return response;
            }

            try
            {
                WebResponse webResponse = endpointRequest.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                int count = webStream.Read(bytes, 0, bytes.Length);

                response.Bytes = bytes;
                response.Response.ReturnCode = 200;

            }
            catch (Exception e)
            {
                response.Response.ReturnCode = 999;
                response.Response.ReturnErrors.Add(new ResponseError("GetArtefactAttachment", e.Message));
            }

            return response;
        }
示例#5
0
        /// <summary>
        /// Updates / creates a listitem.
        /// </summary>
        /// <param name="artefact">Artefact object.</param>
        /// <returns>-1 is error, 0 is successfull update and > 0 is successfull created listitem id.</returns>
        public static ItemResponse SetArtefactFile(ArtefactFile artefact, string contentTypeId)
        {
            ItemResponse response = new ItemResponse();

            // Set REST url
            string url = string.Format("{0}/_api/web/lists/getByTitle('Artefacts')/items", AppSettings.Portal);
            if (artefact.Id > 0)
            {
                url = string.Format("{0}({1})", url, artefact.Id);
            }

            // Create endpointRequest
            HttpWebRequest endpointRequest = (HttpWebRequest)WebRequest.Create(url);
            endpointRequest.Method = "POST";
            endpointRequest.Accept = "application/json; odata=verbose";
            endpointRequest.ContentType = "application/json; odata=verbose";

            var digestResponse = FormDigest.GetFormDigest();
            if (digestResponse.Response.ReturnCode != 200)
                return digestResponse;

            endpointRequest.Headers.Add("X-RequestDigest", digestResponse.Blob);

            if (artefact.Id > 0)
            {
                endpointRequest.Headers.Add("X-HTTP-Method", "MERGE");
                endpointRequest.Headers.Add("IF-MATCH", "*");
            }

            // Set credentials
            //endpointRequest.Credentials = Credentials.GetSharePointCredential();
            response.Response = Credentials.SetSharePointCredential(endpointRequest);
            if (response.Response.ReturnCode != 200)
                return response;

            // Set data
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.Append("  '__metadata': { 'type': 'SP.Data.ArtefactsListItem' } ");

            if (!string.IsNullOrEmpty(contentTypeId))
            {
                sb.AppendFormat(", 'ContentTypeId' : '{0}'", contentTypeId);
            }

            if (!string.IsNullOrEmpty(artefact.Title))
            {
                sb.AppendFormat(", 'Title' : '{0}'", artefact.Title);
            }

            if (!string.IsNullOrEmpty(artefact.FileName))
            {
                sb.AppendFormat(", 'ArtefactFileName' : '{0}'", artefact.FileName);
            }

            //if (!string.IsNullOrEmpty(artefact.SourceId))
            //{
            //    sb.AppendFormat(", 'SourceId' : '{0}'", artefact.SourceId);
            //}

            if (!string.IsNullOrEmpty(artefact.IdNumber))
            {
                sb.AppendFormat(", 'IdNumber' : '{0}'", artefact.IdNumber);
            }

            if (!string.IsNullOrEmpty(artefact.Firstname))
            {
                sb.AppendFormat(", 'Firstname' : '{0}'", artefact.Firstname);
            }

            if (!string.IsNullOrEmpty(artefact.Surname))
            {
                sb.AppendFormat(", 'Surname' : '{0}'", artefact.Surname);
            }

            if (!string.IsNullOrEmpty(artefact.ArtefactId))
            {
                sb.AppendFormat(", 'ArtefactId' : '{0}'", artefact.ArtefactId);
            }

            sb.Append("}");
            endpointRequest.ContentLength = sb.ToString().Length;

            // Execute
            StreamWriter writer = new StreamWriter(endpointRequest.GetRequestStream());
            writer.Write(sb.ToString());
            writer.Flush();
            try
            {
                WebResponse webResponse = endpointRequest.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string reader = responseReader.ReadToEnd();
                if (!string.IsNullOrEmpty(reader))
                {
                    JObject jobj = JObject.Parse(reader);
                    JObject jarr = (JObject)jobj["d"];

                    response.Id = jarr.GetSafeValue<int>("ID");
                    response.Response.ReturnCode = 200;
                }
                else if (!string.IsNullOrEmpty(reader) && artefact.Id == 0)
                {
                    response.Response.ReturnCode = 404;
                    response.Response.ReturnErrors.Add(new ResponseError("Id", "System cannot find the item created."));
                }
            }
            catch (Exception e)
            {
                response.Response.ReturnCode = 999;
                response.Response.ReturnErrors.Add(new ResponseError("SetArtefactFile", e.Message));
            }

            return response;
        }
示例#6
0
 /// <summary>
 /// Updates / creates a listitem.
 /// </summary>
 /// <param name="artefact"></param>
 /// <returns></returns>
 public static ItemResponse SetArtefactFile(ArtefactFile artefact)
 {
     return SetArtefactFile(artefact, null);
 }
示例#7
0
        /// <summary>
        /// Attaches a file to the Artefacts list.
        /// </summary>
        /// <param name="artefact"></param>
        /// <returns></returns>
        public static Response SetArtefactAttachment(ArtefactFile artefact)
        {
            Response response = new Response();

            // Set REST url
            var url = string.Format("{0}/_api/web/lists/GetByTitle('Artefacts')/items({1})/AttachmentFiles/add(FileName='{2}')", AppSettings.Portal, artefact.Id, artefact.FileName);

            // Create endpointRequest
            HttpWebRequest endpointRequest = (HttpWebRequest)WebRequest.Create(url);
            endpointRequest.Method = "POST";
            endpointRequest.Accept = "application/json; odata=verbose";
            endpointRequest.ContentType = "application/json; odata=verbose";

            var digestResponse = FormDigest.GetFormDigest();
            if (digestResponse.Response.ReturnCode != 200)
                return digestResponse.Response;

            endpointRequest.Headers.Add("X-RequestDigest", digestResponse.Blob);
            endpointRequest.Timeout = 1000000;

            byte[] binary = Convert.FromBase64String(artefact.FileBase64);
            endpointRequest.ContentLength = binary.Length;

            // Set credentials
            //endpointRequest.Credentials = Credentials.GetSharePointCredential();
            response = Credentials.SetSharePointCredential(endpointRequest);
            if (response.ReturnCode != 200)
                return response;

            try
            {
                using (System.IO.Stream requestStream = endpointRequest.GetRequestStream())
                {
                    requestStream.Write(binary, 0, binary.Length);
                }

                WebResponse webResponse = endpointRequest.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string reader = responseReader.ReadToEnd();

                response.ReturnCode = 200;
            }
            catch (Exception ex)
            {
                response.ReturnCode = 999;
                response.ReturnErrors.Add(new ResponseError("SetArtefactAttachment", ex.Message));
            }

            return response;
        }