Wednesday, January 25, 2017

How to ensure the file upload succeed using CSOM in Sharepoint Add-ins development?

when we implement the file upload to sharepoint document library. we might not able to log the error from sharepoint, which will cause the the failure of file upload.

we can use the approach that return the document id after the file is uploaded to sharpeoint dcoment library. we can verify the document id to check the result of file upload. if the document id is return, it indicated that the document is successfully upload to sharpeoint. otherwise i will be failed.

here is the snipet of code to handle the above logic.

private string UploadFileToSharepoint(Byte[] fileData, string newFileName)
        {
         
            string guidId = string.Empty;
            try
            {
                var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

                using (var clientAppContext = spContext.CreateAppOnlyClientContextForSPHost())
                {
                    List list = clientAppContext.Web.Lists.GetByTitle(sListTitle);
                    FileCreationInformation newFile = new FileCreationInformation();
                    newFile.Overwrite = false;
                    using (newFile.ContentStream = new MemoryStream(fileData))
                    {
                        newFile.Url = newFileName;
                        Microsoft.SharePoint.Client.File uploadFile = list.RootFolder.Files.Add(newFile);
                        clientAppContext.Load(uploadFile, u => u.ListItemAllFields.Id);
                        clientAppContext.ExecuteQuery();
                        guidId = uploadFile.ListItemAllFields.Id.ToString();
                    }
                    return guidId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Tuesday, January 24, 2017

How to use Select Top N Rows from DB2 Database in AS400 System

it is quite simple if you want to limit the number of records return from the query execution in SQL Server

Select top N Rows from your table..

However DB2 did not support the select Top N Rows, there is a work around function

like this

Select * from your table order by column Desc fetch first N rows only

since the fetch first N rows only will return random number of rows. we must use order by column name to enforece the same records return by sorting the column.

Wednesday, January 18, 2017

How to solve "Collection was modified; enumeration operation may not execute." in Dictionary iteration using C#

when i try to iterate through a dictionary and remove the item that had been processed.

i got a runtime error "Collection was modified; enumeration operation may not execute."

Dictionary<string, FileAttachement> tempAttachment = attachments;
tempAttachment.Remove(item.Key);

the root cause of the error is that i declause a temp variable and reference to the same objec that will iterate through. the run time process detect the change of the attachments object and throw the exeption.

we can't simply use the variable with referenced to dictionary object. we should use the deep copy or clone mechanism to get a copy of the dictionary object.

the following snipet of code will create a new object and copy the content from the dictionary object.

Dictionary<string, FileAttachement> tempAttachment = new Dictionary<string, FileAttachement>(attachments);



Thursday, January 5, 2017

a simple trigger for SQL Server Update Insert and Delete action

i show a easy way to do data auditing with trigger on an Order Table, which will allow user to update and Insert records in the Order table.

here i implement a logic to determine the user action like Update or Insert, if a record is being delete and insert then we know the record had been update, otherwise it is a straight insert

  IF EXISTS(SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED)
    begin
        set @Operation='Update'
    end
   else
   begin
        set @Operation='Insert'
    end


ALTER trigger [dbo].[tblTriggerInsertUpdateAuditRecord] on [dbo].[Order]
after update, insert
as
begin
  declare @Operation nvarchar(10)
  IF EXISTS(SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED)
    begin
        set @Operation='Update'
    end
   else
   begin
        set @Operation='Insert'
    end
  insert into OrderAudit
  ( OrderID
      ,OrderNumber
      ,OrderDate
      ,ShipperID
      ,ShippingCompanyID
      ,WorkStationID
      ,PaymentTermID
      ,CreateDate
      --,UpdateDate
      ,OrderStatus
      ,IsTailGateRequire
      ,AdditionalOrders
      ,TotalPieces
      ,TotalWeight
      ,Supervisor
      ,Operation )
  select  i.OrderID
      ,i.OrderNumber
      ,i.OrderDate
      ,i.ShipperID
      ,i.ShippingCompanyID
      ,i.WorkStationID
      ,i.PaymentTermID
      ,GETDATE()
      ,i.OrderStatus
      ,i.IsTailGateRequire
      ,i.AdditionalOrders
      ,i.TotalPieces
      ,i.TotalWeight
      ,i.Supervisor
      ,@Operation
  from  Order t
  inner join inserted i on t.OrderID=i.OrderID
end













In term of delete, we just need to copy a record from delete table in Temp database

ALTER trigger [dbo].[triggerDeleteAuditOrderDetails] on [dbo].[OrderDetails]
after delete
as
begin
  insert into OrderDetailsAudit
    (OrderDetailsID
      ,SCIOrderID
      ,Quantities
      ,ShipItemID
      ,IsDangerousGoods
      ,DangerousGoodsHazardClassID
      ,Weight
      ,Operation
      ,CreateDate)
  select  D.OrderDetailsID
      ,D.SCIOrderID
      ,D.Quantities
      ,D.ShipItemID
      ,D.IsDangerousGoods
      ,D.DangerousGoodsHazardClassID
      ,D.Weight
      ,'Delete'
      ,GetDate()
  From DELETED
end