Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AsishP/2269a1d8df2bf85e6cb64b67d49b34a9 to your computer and use it in GitHub Desktop.
Save AsishP/2269a1d8df2bf85e6cb64b67d49b34a9 to your computer and use it in GitHub Desktop.
private string appJsonInfo;
private AppDeclaration Apps;
// A class to hold JSON converted objects for Apps
private class AppDeclaration
{
public string appName;
public string appId;
public string clientsideId;
}
// Declare and create a Json Object for all Apps
public void DeclareAppIds
{
appJsonInfo = "[{appId: \"xxxxx-xxxx-xxxx-xxxx-xxxxxxxx\", appName: \"testApp\", clientsideId: \"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx\"}, " +
"{appId: \"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx\", appName: \"testApp1\", clientsideId: \"xxxxxx-xxxx-xxxx-xxxxx-xxxxxx\"}, " +
"{appId: \"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx\", appName: \"testApp2\", clientsideId: \"xxxxxx-xxxx-xxxx-xxxxx-xxxxxx\"}]";
Apps = JsonConvert.DeserializeObject<List<AppDeclaration>>(appJsonInfo);
}
public void AddWebPartsToPage(string pageName)
{
OfficeDevPnP.Core.AuthenticationManager am = new OfficeDevPnP.Core.AuthenticationManager();
using (var cc = am.GetSharePointOnlineAuthenticatedContextTenant(<site url>, UserName, SecurePass))
{
log.Info("Finding and Adding Webpart to page");
ClientSidePage page = ClientSidePage.Load(cc, pageName);
ClientSideComponent clientSideComponent = null;
AppDeclaration WP = Apps.Find(app => app.appName.Contains(<WPSearchString>));
bool controlPresent = false;
bool done = false;
int count = 0;
do
{
try
{
ClientSideComponent[] clientSideComponents = (ClientSideComponent[])page.AvailableClientSideComponents();
foreach (ClientSideComponent csc in clientSideComponents)
{
if (csc.Id.ToLower().Contains(WP.clientsideId))
{
clientSideComponent = csc;
continue;
}
}
foreach (var control in page.Controls)
{
ClientSideWebPart cpWP = control as ClientSideWebPart;
if (cpWP != null && cpWP.SpControlData.WebPartId.ToString() == WP.clientsideId.ToString())
{
controlPresent = true;
done = true;
}
}
if (!controlPresent)
{
ClientSideWebPart WebPart = new ClientSideWebPart(clientSideComponent);
JToken activeValueToken = true;
// Find the web part configuration string from the web part file or code debugging
string propertyJSONString = String.Format("[{{<WP Configuration string>}}]", <parameters>);
JToken propertyTermToken = JToken.Parse(propertyJSONString);
WebPart.Properties.Add("showOnlyActive", activeValueToken);
CanvasSection section = new CanvasSection(page, CanvasSectionTemplate.OneColumn, page.Sections.Count+1);
page.Sections.Add(section);
page.Save();
page.AddControl(WebPart, section.Columns[0]);
page.Save();
page.Publish();
done = true;
controlPresent = true;
}
}
catch(Exception ex)
{
log.Info("Catched exception while adding Capex web part.. Trying again" + ex.Message);
count++;
}
} while (!done && count <=5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment