Search code examples
c#asp.netsharepointmicrosoft-graph-apisharepoint-list

An error occurs when extracting data from a list in a child SharePoint site


Good day. When working with SharePoint, I encountered the need to update a field in the list, for this purpose I use the Microsoft Graph library version 4.54 for c#. I have a method for this:

 {
     try
     {
         int? workflowId = _context.SunApproversWorkflows.FirstOrDefault(el => el.ListGuid == parseXMLDate.ListId).WorkflowId;

         if (workflowId == null)
         {
             return "WorkflowId is null.";
         }

         Uri uri = new Uri(parseXMLDate.WebUrl);
         string listFullName = uri.Host + uri.AbsolutePath;

         var itemFields = await _microsoftGraphService.GetListItemAsync(parseXMLDate.ListId, parseXMLDate.ElementID.ToString(), listFullName);

         var itemId = itemFields.Fields.Id.ToString();

         if (itemId != null && Convert.ToInt32(itemId) == parseXMLDate.ElementID)
         {
             bool hasApprowals365Field = itemFields.Fields.AdditionalData.TryGetValue("Approwals365", out var approwals365FieldObj);
             string approwals365Field = hasApprowals365Field ? approwals365FieldObj?.ToString() : null;

             if (string.IsNullOrEmpty(approwals365Field))
             {
                 var fields = new Dictionary<string, object>
                 {
                     { "Approwals365", $"<a href='https://app.sunsoft.com.ua/ApproversFlow?itemId={itemId}&workFlowID={workflowId}' target=\"_blank\" rel=\"noopener noreferrer\">Approwals365</a>" }
                 };

                 await _microsoftGraphService.UpdateListItemAsync(parseXMLDate.ListId, parseXMLDate.ElementID.ToString(), fields, siteName);

                 return " \nUpdated field Approwals365.\n";
             }
         }

         return " \nField Approvals365 was not updated.\n";
     }
     catch (Exception e)
     {
         return e.Message;
     }
 }

As well as auxiliary methods that serve for communication:

        public async Task<ListItem> GetListItemAsync(string listName, string itemId, string siteName)
        {
            var item = await _graphClient.Sites[siteName].Lists[listName].Items[itemId].Request().Expand("Fields").GetAsync();
            return item;
        }
            public async Task UpdateListItemAsync(string listName, string itemId, Dictionary<string, object> fields, string siteName)
        {
            var updatedFields = new FieldValueSet { AdditionalData = fields };
            await _graphClient.Sites[siteName].Lists[listName].Items[itemId].Fields.Request().UpdateAsync(updatedFields);
        }

This logic works in the parent site, but in child sites, when extracting data about the element, an error occurs: provided identifier is malformed - id is not valid

In test attempts, parseXMLDate.ListId is passed 34ca34e4-dc1f-41b9-846e-a84ecfec012f, parseXMLDate.ElementID.ToString() is passed 1, and listFullName is passed as .sharepoint.com/sites/dev


Solution

  • So in my case it was formatting the site title bar. How I passed the name of the site: .sharepoint.com/sites/TestCarouselKravchenko

    The correct option is: .sharepoint.com:/sites/TestCarouselKravchenko: