简体   繁体   中英

How to use latest SharePoint PnP Core Online in azure function v1

I just follow the instructions as below:

https://github.com/Azure/azure-functions-vs-build-sdk

tried to add new version of newtonsoft.json and then installed the latest SharePointPnPCoreOnline . It works well in my project and also I could do a unit test for my event-grid trigger locally. But after I deploy to azure,an error will happen.It seems the function did not load the proper DLL

Method not found: 'Newtonsoft.Json.Linq.JObject Microsoft.Azure.WebJobs.Extensions.EventGrid.EventGridEvent.get_Data()'.    

and executed this code when error

 [FunctionName("ProcessRequest")]
        [Obsolete]
        public static void Run([EventGridTrigger] string eventGridEvent, TraceWriter log)
        {
            EventGridEvent eventGridEventData = JsonConvert.DeserializeObject<EventGridEvent>(eventGridEvent);
            var siteCreationInfo = eventGridEventData.Data.ToObject<SiteRequest>();
}

very confused about the issue and made all my solutions but could not find the way.

Under this condition and if we have to use both of these libraries,it seems we could not to convert the object to eventgrid object directly.

EventGridEvent eventGridEventData = eventGridEvent.ToObject<EventGridEvent>();

because of the libraries conflict,we can not use this function directly. We should get the key and value separately:

JObject eventGridData = JObject.Parse(eventGridEvent);
        var eventId = eventGridData["id"];
        var siteData = eventGridData["data"];

we should do data conversion in the simple way

The solution to overcoming this issue is by first manually installing a newer version of Newtonsoft.Json via NuGet.

Check the references.

在此处输入图像描述

My test project, it has warnings, but code runs successfully.

在此处输入图像描述

string webTitle = string.Empty;
            JObject jObject = JObject.Parse(@"{
                  'CPU': 'Intel',
                  'Drives': [
                    'DVD read/writer',
                    '500 gigabyte hard drive'
                  ]
                }");
            try
            {
                //Create the client context  
                using (var clientContext = authenticationManager.GetSharePointOnlineAuthenticatedContextTenant(authArray[0], authArray[1], authArray[2]))
                {
                    var web = clientContext.Web;
                    clientContext.Load(web);
                    clientContext.ExecuteQuery();
                    Console.WriteLine(web.Title);
                    webTitle = web.Title;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception : " + ex.Message);
                webTitle = ex.Message;
            }

            return req.CreateResponse(HttpStatusCode.OK, "Hello " + webTitle+ jObject["CPU"]);

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM