Beispiel #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);
            }
        }