public static ArtefactFilesResponse CreateArtefactFilesResponse(List<ArtefactFile> files) { ArtefactFilesResponse response = new ArtefactFilesResponse(); if (files.Count > 0) { response.IdNumber = files[0].IdNumber; response.Firstname = files[0].Firstname; response.Surname = files[0].Surname; response.Status = files[0].Status; response.ArtefactFileList.AddRange(files); } return response; }
/// <summary> /// Searches the Artefacts list. /// </summary> /// <param name="idNumber">Unique filter string.</param> /// <returns></returns> public static ArtefactFilesResponse GetArtefactFiles(ArtefactSearchKey key, string id) { List<ArtefactFile> artefacts = new List<ArtefactFile>(); ArtefactFilesResponse response = new ArtefactFilesResponse(); if (string.IsNullOrEmpty(id)) { response.Response.ReturnCode = 400; response.Response.ReturnErrors.Add(new ResponseError("IdNumber", "Id number cannot be empty.")); return response; } // Set REST url var requestUrl = string.Format("{0}/_api/web/lists/getByTitle('Artefacts')/items", AppSettings.Portal); StringBuilder sb = new StringBuilder(); sb.Append("?$select=ID,ArtefactId,Title,IdNumber,Surname,Firstname,ArtefactFileName"); sb.AppendFormat("&$filter=startswith(ContentTypeId,'{0}')", AppSettings.ArtefactFileContentType); switch (key) { case ArtefactSearchKey.IdNumber: sb.AppendFormat("and IdNumber eq '{0}'", id); break; case ArtefactSearchKey.ArtefactNumber: sb.AppendFormat("and ArtefactId eq '{0}'", id); break; default: // Return all break; } requestUrl = requestUrl + sb.ToString(); // Create endpointRequest HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(requestUrl); endpointRequest.Method = "GET"; endpointRequest.Accept = "application/json;odata=verbose"; // Set credentials //endpointRequest.Credentials = Credentials.GetSharePointCredential(); var ret = Credentials.SetSharePointCredential(endpointRequest); if (ret.ReturnCode != 200) { response.Response = ret; return response; } // Execute HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse(); try { WebResponse webResponse = endpointRequest.GetResponse(); Stream webStream = webResponse.GetResponseStream(); StreamReader responseReader = new StreamReader(webStream); string reader = responseReader.ReadToEnd(); JObject jobj = JObject.Parse(reader); JArray jarr = (JArray)jobj["d"]["results"]; foreach (JObject j in jarr) { artefacts.Add(ArtefactFileHelper.CreateArtefactFile(j)); } responseReader.Close(); // create return object response = ArtefactFileHelper.CreateArtefactFilesResponse(artefacts); response.Response.ReturnCode = artefacts.Count > 0 ? 200 : 404; } catch (Exception e) { response.Response.ReturnCode = 999; response.Response.ReturnErrors.Add(new ResponseError("GetArtefactFiles", e.Message)); } return response; }