The following snippet demonstrates how to implement a timer job and update its progress using SPJobDefinition.UpdateProgress. The example provided below is just to show you how to provision a timer job and update its progress and may not relate well to real-world scenarios.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Administration;
using System.Web.Configuration;
using System.Configuration;
public class AutomatcSiteDescUpdater : SPJobDefinition
{
public AutomatcSiteDescUpdater()
: base()
{
}
public AutomatcSiteDescUpdater(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{
}
public AutomatcSiteDescUpdater(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = “Description Updater”;
}
public override void Execute(Guid contentDbId)
{
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPSiteCollection siteCol = webApplication.Sites;
int siteColCount = siteCol.Count; //In my example (also shown in the picture below) there were 21 site collections.
for (int i = 0; i < siteColCount; i++)
{
SPWeb rootWeb = site.RootWeb;
rootWeb.Description = “Description of this web was last updated on ” + DateTime.Now.ToString();
rootWeb.Update();
//Update Progress Bar
this.UpdateProgress((int)(i * 100 / siteColCount));
//Dispose rootWeb
if (rootWeb != null)
{
try { rootWeb.Dispose(); }
catch { }
}
}
}
Progress gets updated as you refresh the page
Categories: Uncategorized Tags:
SPSecurity exposes a method called “RunWithElevatedPrivileges” which gives you an option to elevate the privilege to the application pool identity under which your code is executing. Looks nice, eh?
But Wait a second!! You are not done yet. I wish it was that easy when it comes to impersonation. In order to get this method call to properly impersonate your application pool identity you need to do some more work. Basically, SPSite and SPWeb objects created outside do not have Full Control even when referenced inside the delegate (anonymous method), so you need to find out their GUID before impersonation is performed and re-create the context one more time. Finally,never forget to dispose your objects!
//Don’t dispose the following two objects. Sharepoint will take care of their disposal when page is completely rendered.
SPWeb webInUserContext = SPContext.Current.Web;
SPSite SiteInUserContext = SPContext.Current.Site;
Guid webGuid = webInUserContext.ID;
Guid siteGuid = SiteInUserContext.ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// get the site in impersonated context
using (SPSite site = new SPSite(siteGuid))
{
// get the web in the impersonated context
SPWeb web = site.OpenWeb(webGuid);
// Do your work here
web.Dispose();
}
});
Categories: Uncategorized Tags:
Earlier today I was informed by Microsoft that I have been awarded an MVP (Most Valuable Professional) in Microsoft Office SharePoint Server 2007. I just wanted to post a great “thanks” to those people who nominated and voted for me especially my former colleague, Eli Ribbilard and my new MVP lead Sasha Krsmanovic. I’d also like to extend my sentiments to my blog readers who read my blog and email me with questions and enlighten me with their warm comments. They truly make me feel to be part of our SharePoint community and keep me well-motivated to contribute as much as possible.
I feel much honored to have received this award and truly enjoy being deeply involved in our community. Thanks Microsoft and everyone!
Categories: Uncategorized Tags:
1) For PDF version of the slides click here.
2) All the code snippets are zipped into a file which can be downloaded from here.
3) Follow up on questions which were not answered:
Question 1) A gentleman mentioned that they are getting “The underlying connection was closed…” after a while when they call a sharePoint web service. I asked him to follow up on this with me later on , but it seems that our guy is so busy so I went ahead and replicated the same problem on my end.As I mentioned in the session too, this behavior can occur for a number of reasons. From not having access to call the web service to not having the proper ACL on the asmx file itself. Fortunately, his problem is more specific because he can call the web service for a while and then it fails so it clearly shows that it is not an access issue. I could replicate the issue when I called the web service from a windows form application which was behind a http proxy server. If I call the same web service not through the HTTP Proxy, the call goes through with no problem and it will stay alive forever – I tested my call the day after and service was still alive. When calling without using the HTTP Proxy, Web service is fine for the first few hours/calls and then it dies and returns the same error message they are getting. Here is my 2 cents on this. I hope this helps:
I solved this issue by altering the generated proxy class. In that generated proxy class there is a method called “GetWebRequest” with the following signature:
protected override System.Net.WebRequest GetWebRequest(Uri uri)
Change this method to:
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest) base.GetWebRequest(uri);
webRequest.KeepAlive = false;
return webRequest;
}
Setting KeepAlive to false may result in sending a “Connection:Close” header to the server each time you initiate the call and I believe that more processing is required on the server, so think twice before sticking to this solution. If you are okay with this performance hit that you may experience then go for it.
Question 2) Second question was made by one my colleagues about UpdateSystem method.SDK clearly states that this method is a kind of silent/anonymous update without leaving any foot trail behind. I can see many places that using this method is proffered over ordinary “Update()”. One of them is when you run a process to adjust some fields in the items of a list and you don’t want the “modified by” and “modified date” to be changed to your process account and the date and time the adjustment was made or even you don’t want to release another version if versioning is in place. That was an interesting one and managed to be added to my top tips for writing better code in sharePoint 🙂
Categories: Uncategorized Tags:
Microsoft Surface will be released this winter and is a breakthrough technology .This amazing technology will bring computers to another level for sure. Surface Computing is no longer a dream. Imagine if this is combined with real time collaboration , then this world would be a lot easier place to live in!
Categories: Uncategorized Tags: