using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
//using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
//using System.Web;
using System.Xml;
using IQS_CommentsView;
namespace IQS_AddwebpartProgramatically
{
class Program
{
static void Main(string[] args)
{
int counter = 0;
Console.WriteLine("Enter the Name of the site");
Console.WriteLine("Enter the Zone");
string ZoneId = Console.ReadLine();
string ListName = "DW Individ List";
string subsite = ""; string url = ""; int intLargestZoneIndex = 0;
Program obj = new Program();
using (SPSite site = new SPSite(""))
{
SPWeb web = site.OpenWeb();
SPList spList = web.Lists[ListName];
SPListItemCollection items = spList.Items;
foreach (SPListItem item in items)
{
try
{
int itmcnt = items.Count;
if (counter < 3)//Set the counter
{
subsite = Convert.ToString(item["Link"]);
string id = Convert.ToString(item["DW Individ No"]);
string[] subsiteurl = subsite.Split(',');
url = subsiteurl[0];
web.AllowUnsafeUpdates = true;
SPLimitedWebPartManager webParts = web.GetLimitedWebPartManager(url + "/default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); //Get webpart Manager
//Create an instance of Custom Webpart and add in a Webpart zone
IQS_CommentsView.IQS_CommentsViewWebPart wp = new IQS_CommentsViewWebPart();
wp.Title = "My WebPart Using Object Model"; //Give Title
intLargestZoneIndex = obj.getLargestZoneIndex(webParts);//Get largest zoneIndex, This is to add webpart to the last of webpart zone
webParts.AddWebPart(wp, ZoneId, intLargestZoneIndex + 1);//If webpart should be added in the begining give third parameter 0(Zone index)
webParts.SaveChanges(wp);
web.Update();
}
}
catch { }
counter++; Console.WriteLine(counter);
}
web.Dispose();
site.Dispose();
}
}
public int getLargestZoneIndex(SPLimitedWebPartManager wpm)
{
int largestIndex = 0; int newIndex = 0;
foreach (var webPartOnPage in wpm.WebParts)//GEt webparts in the page
{
try
{
System.Web.UI.WebControls.WebParts.WebPart webPartObj =
(System.Web.UI.WebControls.WebParts.WebPart)(webPartOnPage);
newIndex = webPartObj.ZoneIndex;
if (largestIndex < newIndex)//Find the largest of ZoneIndex
{
largestIndex = newIndex;
}
}
catch { }
}
return largestIndex;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
//using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
//using System.Web;
using System.Xml;
using IQS_CommentsView;
namespace IQS_AddwebpartProgramatically
{
class Program
{
static void Main(string[] args)
{
int counter = 0;
Console.WriteLine("Enter the Name of the site");
Console.WriteLine("Enter the Zone");
string ZoneId = Console.ReadLine();
string ListName = "DW Individ List";
string subsite = ""; string url = ""; int intLargestZoneIndex = 0;
Program obj = new Program();
using (SPSite site = new SPSite(""))
{
SPWeb web = site.OpenWeb();
SPList spList = web.Lists[ListName];
SPListItemCollection items = spList.Items;
foreach (SPListItem item in items)
{
try
{
int itmcnt = items.Count;
if (counter < 3)//Set the counter
{
subsite = Convert.ToString(item["Link"]);
string id = Convert.ToString(item["DW Individ No"]);
string[] subsiteurl = subsite.Split(',');
url = subsiteurl[0];
web.AllowUnsafeUpdates = true;
SPLimitedWebPartManager webParts = web.GetLimitedWebPartManager(url + "/default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); //Get webpart Manager
//Create an instance of Custom Webpart and add in a Webpart zone
IQS_CommentsView.IQS_CommentsViewWebPart wp = new IQS_CommentsViewWebPart();
wp.Title = "My WebPart Using Object Model"; //Give Title
intLargestZoneIndex = obj.getLargestZoneIndex(webParts);//Get largest zoneIndex, This is to add webpart to the last of webpart zone
webParts.AddWebPart(wp, ZoneId, intLargestZoneIndex + 1);//If webpart should be added in the begining give third parameter 0(Zone index)
webParts.SaveChanges(wp);
web.Update();
}
}
catch { }
counter++; Console.WriteLine(counter);
}
web.Dispose();
site.Dispose();
}
}
public int getLargestZoneIndex(SPLimitedWebPartManager wpm)
{
int largestIndex = 0; int newIndex = 0;
foreach (var webPartOnPage in wpm.WebParts)//GEt webparts in the page
{
try
{
System.Web.UI.WebControls.WebParts.WebPart webPartObj =
(System.Web.UI.WebControls.WebParts.WebPart)(webPartOnPage);
newIndex = webPartObj.ZoneIndex;
if (largestIndex < newIndex)//Find the largest of ZoneIndex
{
largestIndex = newIndex;
}
}
catch { }
}
return largestIndex;
}
}
}