Lists having attachments in SharePoint online c# CSOM.

Amit Rawat 546 Reputation points
2021-02-24T07:30:07.867+00:00

Is it possible with CSOM C# to fetch only those lists having attachments from the website in a single context?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
7,044 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,430 questions
0 comments No comments
{count} vote

Accepted answer
  1. Amos Wu-MSFT 4,041 Reputation points
    2021-02-25T02:52:06.867+00:00

    Attachment is related to the list item, so there is no way to directly determine whether there is an attachment in the list. We can traverse each item in the list and determine whether there is an attachment in the list.

                    // Get all SharePoint lists  
                    ListCollection targetlListCollection = clientContext.Web.Lists;  
      
                    clientContext.Load(targetlListCollection);  
                    clientContext.ExecuteQuery();  
                    CamlQuery oQuery = CamlQuery.CreateAllItemsQuery();  
                    // Iterate through each list object  
                    foreach (List list in targetlListCollection)  
                    {                            
                        if (list.BaseTemplate==100 || list.BaseTemplate == 103 || list.BaseTemplate == 104 || list.BaseTemplate == 105||list.BaseTemplate == 107 || list.BaseTemplate == 106 || list.BaseTemplate == 170)  
                        {  
                            ListItemCollection oCollection = list.GetItems(oQuery);  
                            clientContext.Load(oCollection);  
                            clientContext.ExecuteQuery();  
      
                            foreach (ListItem oItem in oCollection)  
                            {  
      
                                clientContext.Load(oItem.AttachmentFiles);  
                                clientContext.ExecuteQuery();  
      
                                if (oItem.AttachmentFiles.Count > 0)  
                                {  
                                    Console.WriteLine(list.Title);  
                                    Console.WriteLine(oItem.AttachmentFiles.Count);  
                                    break;  
                                }  
                            }  
                        }  
                          
                    }  
    

    Tip: If I miss the list template id that can include an attachment in the code, please add it completely.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful