Archive for 'Programming'
Couple “Gotchas” with Console Applications and SharePoint 2010
Posted on 12. Jul, 2011 by admin.
Wrote a console application today that synchronizes a SQL database with task information from a SharePoint farm as part of a task aggregation solution for a client. Came across two small “issues” with Visual Studio 2010/SharePoint 2010 and console applications. I am primarily writing this to remind myself later, but perhaps it will help someone else as well.
First “gotcha”: Make sure you set the target .NET Framework to 3.5. By default Visual Studio 2010 is going to select 4.0 and it will then do a bunch of complaining about not finding Microsoft.SharePoint.dll. Yes, the compiler will give a pretty detailed error which may or may not reference the fact that you need to target 3.5, but it had me scratching my head for awhile so I figure its worth capturing.
Second “gotcha”: You need to target x64 platform. I was getting all kinds of crazy behavior when targeting x86. When I would try to create SPSite objects I would get “FileNotFound”. If I would try to use SPWebService.Locate I would get permissions issues. Switch the platform to x64 and everything started working like magic!
That’s it, hope this saves someone some time (preferably me on a later project
).
Continue Reading
Programmatically Changing the Show and Hide Ribbon Navigation Setting
Posted on 15. Jun, 2011 by bryan.
Much like everything I blog about, I had a requirement from a customer to programmatically set the “Show and Hide Ribbon” setting that is seen within the Navigation configuration page off of Site Settings (a.k.a. the _layouts/AreaNavigationSettings.aspx page). I looked all through the SPWeb.Navigation options and the PublishingWeb.Navigation options and couldn’t find it. Then I remembered the oldest trick in the book, decompiling SharePoint! I brought up the Microsoft.SharePoint.Publishing.dll in Telerik JustDecompile and found the codebehind implementation for the page. Once there it was easy to figure out how to make the change. Fast-forward, here it is:
SPWeb web = null; // works better if you actually set this to a valid instance // so this property, the __DisplayShowHideRibbonActionId, doesn't exist unless it's set to No! // so in this instance we are setting it to No, but if you want to set it to Yes you just // want to clear the value for the property. make it string.Empty, or just delete the // property entirely. web.AllProperties["__DisplayShowHideRibbonActionId"] = "False"; // Update your web, do a jig. web.Update();
Continue Reading
Programmatically Add ListViewWebPart with Customized View
Posted on 15. Jun, 2011 by bryan.
There are several approaches to adding a ListViewWebPart to a page floating around the Interwebs, and I do believe over the course of the past week I have tried every one of them! What I intend to do here is to cut to the chase and present the method that worked reliably for my purposes.
Scenario
As the title suggests, the scenario is that we are adding a ListViewWebPart to a page, utilizing one of the current list views as a “template”, but making some changes for the purposes of this web part.
Solution
I recommend hitting the “view source” button on the syntax highlighter, I really need to switch to a wider blog template
. In any event, here is the solution that I employed, I added numerous comments to describe why things were being done. Hopefully it saves someone some time.
SPLimitedWebPartManager limitedWebPartManager = null;
ListViewWebPart wp = null;
try
{
// assume that you have a limitedWebPartManager variable containing an
// instance of a SPLimitedWebPartManager class.
// also assume that you have a list variable containing a reference to the
// SPList that you are pointing this ListViewWebPart at.
// create an instance of the ListViewWebPart
wp = new ListViewWebPart();
// convert the list GUID to a string, must include braces (ToString("B")) and be in uppers (ToUpper())
wp.ListName = list.ID.ToString("B").ToUpper();
// optionally set the title of the web part
wp.Title = "I Love List Views";
// add the web part to the limited web part manager. when you do this, a "hidden" view will be created
// on the list referenced by the list variable. we will later get a reference to this hidden view and bend it
// to our will.
limitedWebPartManager.AddWebPart(wp, "whatever web part zone you want to add it to", 1); // the 1 is the index within the zone
// you need to update the list because a view was just added to it
list.Update();
// now that the web part has been added we need to get a fresh reference to it from the limitedWebPartManager.
// there are plenty of great ways to do this, in this example i will be using the low-tech approach of enumerating the
// WebParts collection and checking for the type and title that matches our part. i am quite positive there are more
// efficient ways of doing this, but this code gets called exactly once every couple months, so efficiency isn't worth
// the extra development cycles
foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in limitedWebPartManager.WebParts)
{
// check to see if the type of the web part is ListViewWebPart, and if so, check if the title matches our guy.
if (webPart is ListViewWebPart && webPart.Title.Equals("I Love List Views"))
{
// cast the web part to a ListViewWebPart so we can start to tweak its properties
var listViewWebPart = webPart as ListViewWebPart;
// here is some of the magic. the web part now has its ViewGuid property populated which
// contains the Guid of the hidden view. we get a reference to it here.
var view = list.Views[new Guid(listViewWebPart.ViewGuid)];
// here we grab a reference to a template view. this may be the default view of the list,
// or maybe you have another view that you created that you want to use. you don't have to
// do this, but in this scenario i wanted to copy the filter and sort settings from an existing view.
var templateView = list.Views["Name Of Template View"];
// now we drop all of the view fields in the hidden view
view.ViewFields.DeleteAll();
// now we are going to go through a string array and add each of the strings to the view fields collection. again,
// you don't have to do this, but if you want to change what columns are displayed in your ListViewWebPart, you will
// need to muck with the ViewFields of the view.
Array.ForEach(new string[] { "First Name", "Last Name", "Address" }, f => view.ViewFields.Add(f));
// here we copy the Query straight from the templateView. if you didn't have a templateView you could always
// just create an SPQuery instance and assign it to the view.Query property. no points will be taken off for doing that.
view.Query = templateView.Query;
// update our hidden view, and bask in the awesomeness of our customized ListViewWebPart
view.Update();
}
}
}
finally
{
// do some fun cleanup of disposable items. if you are wondering about the .Web.Dispose() bit look for my
// blog article on the memory leaks in the SPLimitedWebPartManager.
if (limitedWebPartManager != null)
{
if (limitedWebPartManager.Web != null)
{
limitedWebPartManager.Web.Dispose();
}
limitedWebPartManager.Dispose();
}
if (wp != null)
{
wp.Dispose();
}
// i probably missed some .Dispose()s since I cobbled this code together in WordPress and not Visual Studio
}
}
Continue Reading
Subversion “Shelving”
Posted on 07. Jan, 2009 by bryan.
I had one of those moments yesterday where you get 60% of the way through fixing a problem a certain way, and then discover that there was a simpler, more elegant solution that would require 10% of the effort. Problem being, I had just written a lot of code that I didn’t necessarily want to lose forever, incase it may be useful somewhere down the road.
From my days working with Team Foundation Server I remembered the "Shelving" concept. Unfortunately, I was using Subversion, not Team Foundation Server. But isn’t a shelf really just a branch? So I figured I could do a poor-mans shelf with Subversion, and I was right. Worked great.
I was going to write up my steps to do so but decided I would do a quick Google sanity check before expending the effort. Glad I did, cause Mark Phippard did a much better job then I would have done. Kudos Mark, thanks for the excellent amount of detail.
Continue Reading
Great new Visual Studio color theme
Posted on 28. Mar, 2008 by bryan.
The other day Tomas Restrepo released a new Visual Studio Color Theme called Distant Shores. It is a low-contrast theme with a dark background, and I must admit I am a BIG FAN! Have a look at the following screen shot. BTW – The font I am using is Damien Guard’s Envy CodeR font that Tomas Restrepo recommends in his blog posting.
Give it a shot! Your eyes will thank you!
Continue Reading
My First Linq Program
Posted on 18. Jun, 2007 by bryan.
I successfully coded my very first LINQ program today, and man is it a doozie! Observe:
class Program { static void Main(string[] args) { List<Student> studentList = new List<Student>(); studentList.Add(new Student("Charlie", "Brown")); studentList.Add(new Student("Drew", "Carrie")); IEnumerable<Student> students = from Student in studentList where Student.LastName.Equals("Brown") select Student; foreach (Student student in students) { Console.WriteLine(student.FirstName); } Console.ReadLine(); } } internal class Student { private string _firstName; private string _lastName; public Student(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; } public string LastName { get { return _lastName; } set { _lastName = value; } } public string FirstName { get { return _firstName; } set { _firstName = value; } } }
Which when run looks like:
I’m excited by the possibilities this offers, and am anxious to have an opportunity to explore all LINQ has to offer!

