Пример #1
0
        ////// <summary>
        ////// The list received a context event.
        ////// </summary>
        //public override void ContextEvent(SPItemEventProperties properties)
        //{
        //  base.ContextEvent(properties);
        //}

        /// <summary>
        /// Executes the Barista Script in response to an item event.
        /// </summary>
        /// <param name="properties">The properties.</param>
        private static void ExecuteBaristaScript(SPItemEventProperties properties)
        {
            BrewRequest request;

            using (var web = properties.OpenWeb())
            {
                var list = web.Lists[properties.ListId];
                var item = list.GetItemById(properties.ListItemId);

                var headers = new Dictionary <string, IEnumerable <string> >
                {
                    { "Content-Type", new[] { "application/json" } }
                };

                request = new BrewRequest
                {
                    Headers             = new BrewRequestHeaders(headers),
                    Code                = properties.ReceiverData,
                    ScriptEngineFactory =
                        "Barista.SharePoint.SPBaristaJurassicScriptEngineFactory, Barista.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a2d8064cb9226f52"
                };

                request.SetExtendedPropertiesFromSPItemEventProperties(web, list, item, properties);
            }

            var serviceContext = SPServiceContext.Current ??
                                 SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default,
                                                             new SPSiteSubscriptionIdentifier(Guid.Empty));

            var client = new BaristaServiceClient(serviceContext);

            client.Exec(request);
            //TODO: Allow for Syncronous events that expect to return an object with which to update the properties object with.
        }
        protected override void InternalProcessRecord()
        {
            // get the specified service context
            var serviceContext = ServiceContext.Read();

            if (serviceContext == null)
            {
                WriteError(new InvalidOperationException("Invalid service context."), ErrorCategory.ResourceExists, null);
                return;
            }

            if (m_isEval && m_isExec)
            {
                WriteError(new InvalidOperationException("Can be exec or eval, but not both."), ErrorCategory.InvalidArgument, null);
                return;
            }

            if (m_isEval == false && m_isExec == false)
            {
                WriteError(new InvalidOperationException("Must specify either eval or exec"), ErrorCategory.InvalidArgument, null);
                return;
            }

            var client = new BaristaServiceClient(serviceContext);

            var headers = new Dictionary <string, IEnumerable <string> >
            {
                { "Content-Type", new[] { "application/json" } }
            };

            var request = new BrewRequest
            {
                Code                = m_code,
                Headers             = new BrewRequestHeaders(headers),
                ScriptEngineFactory = m_scriptEngineFactory
            };

            if (m_isEval)
            {
                var response = client.Eval(request);

                if (response.ContentType.StartsWith("application/json", StringComparison.InvariantCultureIgnoreCase) ||
                    response.ContentType.StartsWith("application/xml", StringComparison.InvariantCultureIgnoreCase) ||
                    response.ContentType.StartsWith("application/javascript", StringComparison.InvariantCultureIgnoreCase) ||
                    response.ContentType.StartsWith("text", StringComparison.InvariantCultureIgnoreCase))
                {
                    WriteResult(Encoding.UTF8.GetString(response.Content));
                }
                else
                {
                    WriteObject(response.Content);
                }
            }
            else
            {
                client.Exec(request);
            }
        }
Пример #3
0
        protected override void CreateChildControls()
        {
            if (String.IsNullOrEmpty(Code))
            {
                return;
            }

            if (String.IsNullOrEmpty(Code.Trim()))
            {
                return;
            }

            BaristaHelper.EnsureExecutionInTrustedLocation();

            string codePath;
            var    codeToExecute = Tamp(Code, out codePath);

            var client = new BaristaServiceClient(SPServiceContext.Current);

            var request = BrewRequest.CreateServiceApplicationRequestFromHttpRequest(HttpContext.Current.Request);

            request.ScriptEngineFactory = "Barista.SharePoint.SPBaristaJurassicScriptEngineFactory, Barista.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a2d8064cb9226f52";
            request.Code     = codeToExecute;
            request.CodePath = codePath;

            var headers = new Dictionary <string, IEnumerable <string> >
            {
                { "barista_instancemode", new[] { InstanceMode.ToString() } },
                { "barista_instancename", new[] { InstanceName } },
                { "barista_instanceabsoluteexpiration", new[] { InstanceAbsoluteExpiration.ToString() } },
                { "barista_instanceslidingexpiration", new[] { InstanceSlidingExpiration.ToString() } }
            };

            if (String.IsNullOrEmpty(InstanceInitializationCode) == false)
            {
                string filePath;
                request.InstanceInitializationCode     = Tamp(InstanceInitializationCode, out filePath);
                request.InstanceInitializationCodePath = filePath;
            }
            request.Headers = new BrewRequestHeaders(headers);
            request.SetExtendedPropertiesFromCurrentSPContext();

            var result     = client.Eval(request);
            var resultText = System.Text.Encoding.UTF8.GetString(result.Content);

            //TODO: Based on the content type of the result, emit the contents differently.
            var cntrl = new LiteralControl(resultText);

            Controls.Add(cntrl);
        }
Пример #4
0
        public async Task<ActionResult> BuyCoffeeWcf()
        {
            var myOrder = new PlaceOrder
            {
                Coffee = Coffee.Espresso,
                OrderNumber = Guid.NewGuid()
            };

            var proxy = new BaristaServiceClient();
            ErrorCodes result = (ErrorCodes)await proxy.PutAsync(myOrder);

            if (result != ErrorCodes.Success)
            {
                throw new RequestFailedException(result);
            }

            return View("BuyCoffee", new OrderReadyViewModel(myOrder));
        }
Пример #5
0
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Result = String.Empty;

            // get all of the information we currently have about the item
            // that this workflow is running on
            var listGuid = new Guid(__ListId);
            var web      = __Context.Web;
            var myList   = __Context.Web.Lists[listGuid];
            var myItem   = __Context.GetListItem(myList, __ListItem);

            var serviceContext = SPServiceContext.GetContext(__Context.Site);
            var client         = new BaristaServiceClient(serviceContext);

            var headers = new Dictionary <string, IEnumerable <string> >
            {
                { "Content-Type", new[] { "application/json" } }
            };

            var request = new BrewRequest
            {
                Code                = Code,
                Headers             = new BrewRequestHeaders(headers),
                Body                = Encoding.UTF8.GetBytes(Body),
                ScriptEngineFactory = "Barista.SharePoint.SPBaristaJurassicScriptEngineFactory, Barista.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a2d8064cb9226f52",
                ExtendedProperties  = new Dictionary <string, string> {
                    { "SPSiteId", __Context.Site.ID.ToString() },
                    { "SPUrlZone", __Context.Site.Zone.ToString() },
                    { "SPUserToken", Convert.ToBase64String(__Context.Site.UserToken.BinaryToken) },
                    { "SPWebId", web.ID.ToString() },
                    { "SPListId", __ListId },
                    { "SPListItemUrl", myItem.Url },
                    { "SPWorkflowAssociationTitle", __Context.AssociationTitle },
                    { "SPWorkflowInstanceId", __Context.WorkflowInstanceId.ToString() },
                    { "SPWorkflowCurrentItemUrl", __Context.CurrentItemUrl },
                    { "SPWorkflowCurrentWebUrl", __Context.CurrentWebUrl },
                    { "SPWorkflowItemName", __Context.ItemName },
                    { "SPWorkflowTaskListGuid", __Context.TaskListGuid },
                    { "SPWorkflowStatusUrl", __Context.WorkflowStatusUrl },
                    { "SPWorkflowAssociatorUserLoginName", __Context.AssociatorUser.LoginName },
                    { "SPWorkflowInitiatorUserLoginName", __Context.InitiatorUser.LoginName },
                    { "SPWorkflowItemId", __Context.ItemId.ToString(CultureInfo.InvariantCulture) },
                    { "SPWorkflowStartedDateTime", __Context.StartedDateTime.ToUniversalTime().ToString(@"yyyy-MM-ddTHH\:mm\:ss.fffffffzzz") },
                    { "SPWorkflowLastRunDateTime", __Context.LastRunDateTime.ToUniversalTime().ToString(@"yyyy-MM-ddTHH\:mm\:ss.fffffffzzz") }
                }
            };

            var response = client.Eval(request);

            if (response.StatusCode == HttpStatusCode.BadRequest && response.ExtendedProperties.ContainsKey("Exception_Message"))
            {
                var wfService = executionContext.GetService <ISharePointService>();

                wfService.LogToHistoryList(executionContext.ContextGuid,
                                           SPWorkflowHistoryEventType.WorkflowError,
                                           0,
                                           TimeSpan.Zero,
                                           "Message",
                                           response.ExtendedProperties["Exception_Message"],
                                           "");

                wfService.LogToHistoryList(executionContext.ContextGuid,
                                           SPWorkflowHistoryEventType.WorkflowError,
                                           0,
                                           TimeSpan.Zero,
                                           "Name",
                                           response.ExtendedProperties["Exception_Name"],
                                           "");

                wfService.LogToHistoryList(executionContext.ContextGuid,
                                           SPWorkflowHistoryEventType.WorkflowError,
                                           0,
                                           TimeSpan.Zero,
                                           "Function Name",
                                           response.ExtendedProperties["Exception_FunctionName"],
                                           "");

                wfService.LogToHistoryList(executionContext.ContextGuid,
                                           SPWorkflowHistoryEventType.WorkflowError,
                                           0,
                                           TimeSpan.Zero,
                                           "Line Number",
                                           response.ExtendedProperties["Exception_LineNumber"],
                                           "");

                wfService.LogToHistoryList(executionContext.ContextGuid,
                                           SPWorkflowHistoryEventType.WorkflowError,
                                           0,
                                           TimeSpan.Zero,
                                           "Source Path",
                                           response.ExtendedProperties["Exception_SourcePath"],
                                           "");

                wfService.LogToHistoryList(executionContext.ContextGuid,
                                           SPWorkflowHistoryEventType.WorkflowError,
                                           0,
                                           TimeSpan.Zero,
                                           "Stack Trace",
                                           response.ExtendedProperties["Exception_StackTrace"],
                                           "");

                return(ActivityExecutionStatus.Faulting);
            }

            Result = Encoding.UTF8.GetString(response.Content);


            return(ActivityExecutionStatus.Closed);
        }