Share via

download sharepoint webpart programmatically.

Emmanuel Dreux 16 Reputation points
2022-08-08T14:24:06.483+00:00

We are migrating webparts programmatically.
We used to use this code in the past but it is not working with modern authentication.

Can you share piece of code for doing this when legacyauth is turned off?

private string DownloadFile(string url)
{
SecureString theSecureString = new NetworkCredential("", _password).SecurePassword;
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(_username, theSecureString);
_authCookieValue = onlineCredentials.GetAuthenticationCookie(new Uri(fileUrl));

using (CookieAwareWebClient client = new CookieAwareWebClient())
{
client.CookieContainer = new CookieContainer();
client.CookieContainer.Add(new Cookie("SPOIDCRL", _authCookieValue.TrimStart("SPOIDCRL=".ToCharArray()), String.Empty, authority));
string localPath = Path.GetTempPath();
string temporaryGuid = Guid.NewGuid().ToString();
string path = System.IO.Path.Combine(localPath, temporaryGuid);
client.DownloadFile(fileUrl, path);
document = System.IO.File.ReadAllText(path);
System.IO.File.Delete(path);
}
}
etc...
}

string itemWebPartDefUrl = string.Format(
"{0}{1}/_vti_bin/exportwp.aspx?pageurl={2}&guidstring={3}",
_rootPrefixURL,
webServerRelativeUrl,
pageURL,
wpDef.Id.ToString().ToLower()
);
Uri targetSite = new Uri(_context.Web.Url);
string xmlDef = _directLayer.DownloadFile(itemWebPartDefUrl, targetSite.Authority);

Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft 365 and Office | SharePoint Server | Development
0 comments No comments

2 answers

Sort by: Most helpful
  1. Emmanuel Dreux 16 Reputation points
    2022-08-24T11:38:57.62+00:00

    Many webparts are marked as not exportable, therefore the solution does not give entire satisfaction, but that's ok.

    Was this answer helpful?


  2. RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
    2022-08-09T02:34:10.393+00:00

    Hi @Emmanuel Dreux ,
    Exporting Web Parts using CSOM is very easy and comes down to calling the LimitedWebPartManager.ExportWebPart method passing the ID of the Web Part to export as its parameter:

    using (ClientContext ctx = new ClientContext("https://contoso.sharepoint.com")) {  
      SecureString password = new SecureString();  
      // setting password omitted for brevity  
      ctx.Credentials = new SharePointOnlineCredentials("******@contoso.onmicrosoft.com", password);  
      File file = ctx.Web.GetFileByServerRelativeUrl("/pages/default.aspx");  
      LimitedWebPartManager wpMgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);  
      Guid webPartId = new Guid("468a7c31-053b-41b3-9e7e-d658a3ecb7fc");  
      ClientResult<string> webPartXml = wpMgr.ExportWebPart(webPartId);  
      ctx.ExecuteQuery();  
      
      // Web Part XML: webPartXml.Value  
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.


    Was this answer helpful?


Your answer

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