using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace AddEWATool
{
/// <summary>
/// Program class
/// </summary>
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
/// <param name="args">arguments</param>
/// <returns>int</returns>
[STAThread]
public static int Main(string[] args)
{
//Application.EnableVisualStyles();
if (args.Length == 0)
{
Application.Run(new Form1());
return 1;
}
else
{
Commandline comm = new Commandline();
int worked = comm.CommandLineAddWebPart(args);
return worked;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Excel.WebUI;
namespace AddEWATool
{
/// <summary>
/// Form1 class derived from System.Windows.Forms
/// </summary>
public partial class Form1 : Form
{
private string siteurl;
private string bookuri;
private bool succeeded;
private string appname = "AddEWATool";
private string specifyinput = "Please add a site URL,
for example, http://myserver/site/";
private string siteproblem = "There was a problem with
the site name. Please check that the site exists.";
private string addproblem = "There was a problem adding the
Web Part.";
private string success = "Web Part successfully added.";
private SPSite site;
private SPWeb TargetWeb;
private SPWebPartCollection sharedWebParts;
/// <summary>
/// Class Constructor
/// </summary>
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Method to add the Excel Web Access Web Part
/// </summary>
/// <param name="sitename">URL of the
///Windows SharePoint Services site</param>
/// <param name="book">URI to the workbook</param>
/// <returns>bool</returns>"
public bool AddWebPart(string sitename, string book)
{
bool b = false;
progressBar1.Visible = true;
progressBar1.Minimum = 1;
progressBar1.Maximum = 4;
progressBar1.Value = 1;
progressBar1.Step = 1;
if (String.IsNullOrEmpty(sitename))
{
MessageBox.Show(
specifyinput,
appname,
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
return b;
}
try
{
site = new SPSite(sitename);
TargetWeb = site.OpenWeb();
}
catch (Exception exc)
{
MessageBox.Show(
siteproblem + "\n" + exc.Message,
appname,
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
progressBar1.Value = 1;
return b;
}
progressBar1.PerformStep();
//Get the collection of shared Web Parts
//on the home page
//Log.Comment("Get the collection of
//personal Web Parts on default.aspx");
try
{
sharedWebParts =
TargetWeb.GetWebPartCollection("Default.aspx",
Microsoft.SharePoint.WebPartPages.Storage.Shared);
}
catch (Exception exc)
{
MessageBox.Show(
siteproblem + "\n" + exc.Message,
appname,
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
progressBar1.Value = 1;
return b;
}
progressBar1.PerformStep();
//Instantiate Excel Web Access Web Part
//Add an Excel Web Access Web Part in a shared view
ExcelWebRenderer ewaWebPart = new ExcelWebRenderer();
progressBar1.PerformStep();
ewaWebPart.ZoneID = "Left";
ewaWebPart.WorkbookUri = book;
try
{
sharedWebParts.Add(ewaWebPart);
}
catch (Exception exc)
{
MessageBox.Show(
addproblem + "\n" + exc.Message,
appname,
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
progressBar1.Value = 1;
return b;
}
progressBar1.PerformStep();
b = true;
return b;
}
/// <summary>
/// Button1 click handler
/// </summary>
/// <param name="sender">caller</param>
/// <param name="e">event</param>
private void AddEWAButton_Click(object sender,
EventArgs e)
{
siteurl = textBox1.Text;
bookuri = textBox2.Text;
succeeded = AddWebPart(siteurl, bookuri);
if (succeeded)
{
MessageBox.Show(
success,
appname,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
progressBar1.Value = 1;
}
}
}