Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

context.ExecuteQuery() not working from CSOM Code Activity in Visual Studio 2013

$
0
0

Hello, I have created a Code Activity Block as below. The problem is that as soon as the code encounters the context.ExecuteQuery() statement, it terminates and does not return the desired result. I am using this code activity block inside the workflow.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.WorkflowServices;
using Microsoft.SharePoint.Client.WorkflowServices;
using Microsoft.SharePoint.Client.Workflow;
using System.Net;

namespace ExcelDataConsolidation
{

    public sealed class DataConsolidation : CodeActivity
    {
        // Define the global variables
        string siteUrl;
        string libraryName;
        string listName;
        string localPath;
        string fileName;
        string listItemId;
        string fileCreation;
        /// <summary>This section is for defining the input arguments. In the code block below 5 input activity variables are defined
        /// <para> Site Url, Library Name, List Name, Local File Path and File Names are 5 variables </para>
        /// </summary>
        // Define an activity input argument of type string
        public InArgument<string> SiteUrl { get; set; }
        public InArgument<string> LibraryName { get; set; }
        public InArgument<string> ListName { get; set; }
        public InArgument<string> LocalPath { get; set; }
        public InArgument<string> ListItemId { get; set; }
        public OutArgument<string> OutMessage { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
         protected override void Execute(CodeActivityContext context)
         {
             // Obtain the runtime value of the Text input argument
             siteUrl = context.GetValue(this.SiteUrl);
             libraryName = context.GetValue(this.LibraryName);
             listName = context.GetValue(this.ListName);
             localPath = context.GetValue(this.LocalPath);
             listItemId = context.GetValue(this.ListItemId);

             //Create the context
             IWorkflowContext wfcontext = executionContext.GetExtension<IWorkflowContext>();
             IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
             IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
             try
             {
                 using (ClientContext ctx = new ClientContext(siteUrl))
                 {
                     //FileInformation f = 
                         DownloadDocument(ctx, libraryName, Convert.ToInt32(listItemId), context);
                     //using (var fileStream = new FileStream(localPath + "Data.xls", FileMode.Create))
                     //OutMessage.Set(context, "Starting to copy details to the local file");
                     //using (FileStream fs = System.IO.File.Create(@"c:\mauli\data.xls"))
                     //{
                     //   // OutMessage.Set(context, "Created local file");
                     //   // OutMessage.Set(context, "Copying details");
                     //    f.Stream.CopyTo(fs);
                     //}
                 }
             }
             catch (Exception ex) { }
         }

         public void DownloadDocument(ClientContext context, string libraryName, int itemId, CodeActivityContext codecontext)
         {
             context.Credentials = new NetworkCredential("svc_spp_WebApps_tcs", "Brookfieldtcs", "Bizdev");
             List docList = context.Web.Lists.GetByTitle(libraryName);


             CamlQuery query = new CamlQuery();
                 //CamlQuery.CreateAllItemsQuery();
             //query.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>" + listItemId + @"</Value></Eq></Where></Query></View>";
             //ListItemCollection lstCol = docList.GetItems(query);

             ListItem listitem = docList.GetItemById(itemId);
             context.Load(docList);
             context.Load(listitem);
context.ExecuteQuery();
             Microsoft.SharePoint.Client.File fileInfo = listitem.File;
                     string file = listitem["FileRef"].ToString();
                     string[] fileName1 = file.Split('/');
                     fileName = fileName1[fileName1.Length - 1];
                     FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileInfo.ServerRelativeUrl);
             OutMessage.Set(codecontext, fileName);
             return fInfo;
         }
    }
}
I am struggling since last 3 days, but not able to get a proper solution. Please help


nkumar


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>