Read from SharePoint

Jerry Xu-MSFT 7,956 Reputation points
2020-10-27T07:30:49.08+00:00

Hello there,

I'm using SharePoint online and have successfuly created site with some text webparts. Then I connected this site with c# CSOM.. And there come the question. Is there any options to loop through all text webparts in my site and save the actual text into strings?

Hope you can undestrand my problem.

Thank you all for answer and stay safe.

Benjamin

Source link from TechNet

SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,935 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Amos Wu-MSFT 4,051 Reputation points
    2020-10-28T08:07:51.697+00:00

    My test code for your reference(You need to add Microsoft.SharePointOnline.CSOM and sharepoint pnp core online reference):
    35639-image.png

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Net;  
    using System.Security;  
    using System.Text;  
    using System.Threading.Tasks;  
    using Microsoft.SharePoint.Client;  
    using OfficeDevPnP.Core.Pages;  
    namespace CSOMconnectSPO  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                string userName = "amos@contoso.onmicrosoft.com";  
                Console.WriteLine("Enter your password.");  
                SecureString password = GetPassword();  
                // ClienContext - Get the context for the SharePoint Online Site    
                // SharePoint site URL - https://c986.sharepoint.com    
                using (var clientContext = new ClientContext("https://contoso.sharepoint.com/sites/dev"))  
                {  
                    // SharePoint Online Credentials    
                    clientContext.Credentials = new SharePointOnlineCredentials(userName, password);  
                    // Get the SharePoint web    
                    Web web = clientContext.Web;  
                    // Load the Web properties    
                    clientContext.Load(web);  
                    // Execute the query to the server.    
                    clientContext.ExecuteQuery();  
                    // Web properties - Display the Title and URL for the web    
                    Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url);  
                    ClientSidePage page = ClientSidePage.Load(clientContext, "sitepage.aspx");  
                    List<CanvasControl> controls = page.Controls;  
                    foreach (CanvasControl control in controls)  
                    {                          
                        if(4 == control.ControlType)  
                    {  
                        Console.WriteLine((control as ClientSideText).Text);//content in text web part  
                    }  
                    }  
                    Console.ReadLine();  
                }  
            }  
            private static SecureString GetPassword()  
            {  
                ConsoleKeyInfo info;  
                //Get the user's password as a SecureString    
                SecureString securePassword = new SecureString();  
                do  
                {  
                    info = Console.ReadKey(true);  
                    if (info.Key != ConsoleKey.Enter)  
                    {  
                        securePassword.AppendChar(info.KeyChar);  
                    }  
                }  
                while (info.Key != ConsoleKey.Enter);  
                return securePassword;  
            }  
        }  
    }  
    

    This is just to get the value of the text web part in a page, you can get the value of the text web part in all pages through a loop.


    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.