Archive

Posts Tagged ‘SharePoint Object Model’

Add properties to visual webparts sharepoint 2010

November 7, 2013 Leave a comment

Hi,
This post talks on how one can add custom properties to their SharePoint 2010 visual webparts. Microsoft has brought visual webparts for easing the developer life with SharePoint 2010 development. But when it comes to adding some custom properties for these webparts then there is bit of trouble hidden.
To troubleshoot this issue lets us dive in entities inside webpart project.

  • Inside the webparts folder of the project one can see following files (considering webpart name as “VisualWebPart1”):
  1. Webpart class “VisualWebPart1.cs” that is derived from webpart class
  2. User control file “VisualWebPart1UserControl.ascx” and its associated designer and .cs files

Since we think that merely writing the property and setting its attributes to WebBrowsable=true & Personalizable inside the usercontrol class (VisualWebPart1UserControl.ascx.cs) file will go ahead and add properties into webpart, but in reality this will not work.
So how we gonna do this?
Even though the above assumption of ours is true but its partially true. The fact is, before adding this property to user control class we should
http://www.lamber.info/post/2010/05/21/How-do-I-create-custom-properties-in-Visual-Web-Parts.aspx
http://onlinecoder.blogspot.in/2011/04/custom-properties-in-sharepoint-2010_7158.html

Get SPweb, SPList or Document Library from SPContentDatabase Class

April 5, 2011 1 comment

Hi,

This post is helpful for those who are trying to extract information from the SPContentDatabase class. This is usually required when one is creating custom timer job, base level features etc.

The following code is written for Custom timer job:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration; (must)
using Microsoft.SharePoint;

namespace TTKTimerJob
{

class TTKIssueTimerJob: SPJobDefinition
{

public TTKIssueTimerJob()
: base()
{ }

public TTKIssueTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{ }

public TTKIssueTimerJob(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = “TTK Issue Timer Job”;
}

public override void Execute(Guid targetInstanceId)
{

// get a reference to the current site collection’s content database
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
// Get the TimerJobList Name from the webApplication collection
SPSiteCollection timerSiteCollection = webApplication.ContentDatabases[targetInstanceId].Sites;
SPList timerJobList = null;
foreach (SPSite siteItem in timerSiteCollection)
{
#region code here: finds the List in rootweb
//assumes the existence of a site collection with an absolute URL of
//http://localhost/sites/sitecollection/subsite/Lists/abc and that this site collection has a Web site named subsite
//SPList xyz = siteItem.RootWeb.GetList(“list url here”);

//SPList xyz = siteItem.RootWeb.Lists.TryGetList(“list Title here”);
timerJobList = siteItem.RootWeb.Lists.TryGetList(“ListTimerJob”);
if (timerJobList != null)
{
// create a new list Item, set the Title to the current day/time, and update the item
SPListItem newList = timerJobList.Items.Add();
newList[“Title”] = DateTime.Now.ToString();
newList.Update();
break;
}
#endregion}

}

}

}