Showing posts with label CSOM. Show all posts
Showing posts with label CSOM. Show all posts

Tuesday, April 25, 2017

How to limited number of Rows in return in Data Loading with SharePoint CSOM?

when we load list item from a large document library, the process will hang due to large volumn of items.

it is quite easy to improve the performance to limited the number of rows return from the query. we just need to set the ViewXml property from teh CamlQuery object.

limitedQuery.ViewXml = "<View><RowLimit>50</RowLimit></View>";


 class Program
    {
        static void Main(string[] args)
        {

            string siteUrl = "http://SharepointSever";

            SP.ClientContext clientContext = new SP.ClientContext(siteUrl);

            SP.List oList = clientContext.Web.Lists.GetByTitle("My Documents");
            SP.CamlQuery limitedQuery = new SP.CamlQuery();
            limitedQuery.ViewXml = "<View><RowLimit>50</RowLimit></View>";

            SP.ListItemCollection collListItem = oList.GetItems(limitedQuery);

            clientContext.Load(collListItem);

            clientContext.ExecuteQuery();

            foreach (SP.ListItem oListItem in collListItem)
            {
                Console.WriteLine("ID: {0} \nTitle: {1}", oListItem.Id, oListItem["Title"]);
            }
            Console.ReadLine();

        }
    }

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;
            }
        }