Friday, 24 February 2017

Upload a file in to Filecabinet using Webservice

First we need to browse the file using asp:FileUpload control. 
Here is the code :-

 <div>
     <asp:FileUpload ID="fileupload" runat="server" />         
     <asp:Button ID="btnUpload" runat="server" Text="UPLoad"  onclick="btnUpload_Click" />
</div>

Then in the code behind of the button click event we need to write the following code to get the file information:-

//Getting the file path
string path = System.IO.Path.GetFullPathfileupload.PostedFile.FileName;
//getting the file name
string filename = System.IO.Path.GetFileName(fileupload.FileName);                  
//Getting the file content                    
byte[] fileContent = System.IO.File.ReadAllBytes(path);                    
lblSummary.Text = "Upload status: File uploaded!";
//calling the function to add the file to the file cabinet  
AddFile(filename, fileContent); 

In  AddFile(filename, fileContent) we need to search the specific folder in the file cabinet through suitetalk.
Code to search for the specific folder in the file cabinet :

//Contains details on the status of the operation and a reference to the updated 
//record.WriteResponse writeResp = null; 
// To implement a search for Folder through suitetalk , 
//we need to work with FolderSearch and FolderSearchBasic classes.
FolderSearch fldSearch = new FolderSearch();
FolderSearchBasic fldSrearchBasic = new FolderSearchBasic(); 
SearchStringField searchFolder = null;
              
//Name of the folder that we want to search                 
string searchValue = "Demo";                 
//Filtering by the folder name                 
searchFolder = new SearchStringField();
searchFolder.@operator =SearchStringFieldOperator.@is;
//Search operator
searchFolder.operatorSpecified = true;
searchFolder.searchValue = searchValue;
//searchValue is name of the folder "Demo"
fldSrearchBasic.name =searchFolder ;
fldSearch.basic = fldSrearchBasic;
// Search by isActive field which is a boolean
SearchBooleanField isActive = new SearchBooleanField();                 
isActive.searchValue = true;
isActive.searchValueSpecified = true;
// Invoke search() web services operation
SearchResult response = nsService.search(fldSearch); 

Once the search is successful we need to fetch the information from the search result to add the file to the specific folder in filecabinet.


//Now stroring the search result in an array
Record[] arrRecord = (Record[])response.recordList;
// Process response
if (response.status.isSuccess){
lblSummary.Text = "search is successful";
Folder fldr = (Folder)arrRecord[0];
//Getting a specific folder from the search
string fldrId = fldr.internalId;
//Fetching the required folder's internal ID
RecordRef refId = new RecordRef();    
refId.internalId = fldrId;
//Storing the folder id
//File object created to get all the file information 
File objFile = new File(); 
objFile.name = filename; //setting the file name 
objFile.folder = refId;  //setting the folder information  
objFile.content = fileContent; //settig the file content     
writeResp = nsService.add(objFile); //adding file to the file cabinet  
}else{           
          lblSummary.Text = "search not found"; 
          return;   
}









No comments:

Post a Comment