Tag Archives: sharepoint lists

Creating a Simple Connection Consumer in SharePoint

There are a million articles about using seven different interfaces and fourteen .wsp deployments to make an entirely custom connection provider and consumer. However, I couldn’t find one about how to create a simple connection consumer that filters based on a SharePoint list. I also couldn’t find anything about changing the brand-new double-headed arrow icon that SharePoint replaced the radio button with. Turns out they can go in the same simple solution:

  • Setting up the Consumer – This consumer will take a single row of information from a SharePoint list. It’s implemented in “MyWebPart.cs” and not in a user control.
//Create the data row that the list information will fill
DataRowView _row = null;
//Declare your consumer. Note that "Row" can be anything - it's just the term that
// SP will use when you enable the connection
[ConnectionConsumer("Row")]
//Set up the actual connection
public void SetConnect(IWebPartRow provider)
{
    //"RecieveRow" is the method you'll create to interpret the data
     RowCallback callback = new RowCallback(ReceiveRow);
    //This is where the data comes in 
     provider.GetRowData(callback);
}

This code sets up the DataRow. Note that the “provider” parameter is passed by the SharePoint connection.

  • Capturing your data – This is where you can actually use the filter – manipulate, save, and query to your heart’s content. Below is a sample RecieveRow method:
public void ReceiveRow(object row)
{
     //Set your local _row equal to the row passed by SharePoint
     _row = (DataRowView)row;
   
     //Make sure it isn't null 
     if (_row != null)
     {
    //The next three lines of code are a great way to open a site without having to dispose of anything!
    SPWeb contextWeb = SPControl.GetContextWeb(Context);
    using (SPSite site = new SPSite(contextWeb.Url))
    {
        using (SPWeb web = site.OpenWeb())
        {
         //Convert the DataRowView into a DataRow, so you can actually use it
             DataRow dr = _row.Row;
            
             //I need to allow unsafe updates to execute my query
             web.AllowUnsafeUpdates = true;
             //Query against the row data
             SPQuery query = new SPQuery();
             query.Query = string.Format(
                 @"<Where>
                     <Eq>
                         <FieldRef Name='MyColumn' />
                         <Value Type='Lookup'>{0}</Value>
                     </Eq>
                 </Where>", dr["mycolumnvalue"].ToString() );
                    
             SPList list = web.Lists["MyList"];
             SPListItemCollection items = list.GetItems(query);
            /*********
             *In here you do all you want with that filtered item list.
             *Convert it to a data table, pass the list to a usercontrol.
             *After all, it's your filter!
             ********/
           
            //Disable unsafe updates when you're done
             web.AllowUnsafeUpdates = false;
        }
     }
     }
}

That’s all there is to it. You can fill a data structure in the RecieveRow method and pass it on to a user control the same way you would pass any other value.

  • Customization –  Here’s a little bonus – how to update the radio buttons with the filter wsp.
    • In the hive, the double-headed arrow radio buttons are the following two files:
      • RBSEL.gif
      • RBUNSEL.gif
    • If you want to replace them create a folder in your solution package with the following path:  MyWebPartProject > TEMPLATE > IMAGES
    • Rename your “on” radio button “RBSEL” and save it as a gif
    • Rename your “off” radio button “RBUNSEL” and save it as a gif
    • Place both of them in the IMAGES folder.
    • When you deploy, it will overwrite the default arrows.

This change OVERWRITES the default SharePoint images. Only do this if you want to update all of the radio buttons on the farm. Otherwise you will have to restore the double-headed arrow icons, and it won’t be fun.

 

[Image via the San Francisco Weekly]

NEUGS Part 1: Welcome to the SharePoint Jungle

Before I came to SoftArtisans, I’d never heard of SharePoint. (You can gasp here or save it for later in the post.) As is my wont, I began using it without ever reading any documentation or general how-it-works-for-essentially-tech-illiterate-fools-type information. Which, in terms of doing most of what I need to do (uploading docs to libraries and writing blog posts on my My Site), is not the worst strategy, but it left a lot of gaps. So, with Ben’s encouragement, I recently began a comprehensive SharePoint-for-the-End-User curriculum. And, to my surprise and chagrin, found that there really isn’t one. Don’t get me wrong, End User SharePoint is an amazing resource—but I’d say it’s more tailored to post-bacs. Microsoft used to have a series of training videos, but they seem to be down at the moment, and their getting started articles are pretty skimpy early on and fragmented after the ABCs. So, like any great innovator (if you’ve been holding in that gasp, you can let it out now), I decided to create my own guide. Welcome to part one of many: What SharePoint does for me and which of its parts I will use. Continue reading NEUGS Part 1: Welcome to the SharePoint Jungle

Targeting a SharePoint Site in a Custom Action

In my initial attempt to create a custom action for a SharePoint list, I specified it as follows:

<CustomAction
    Id="SoftArtisans.Tutorial.MenuItemExcel"
    GroupId="ActionsMenu"
    Location="Microsoft.SharePoint.StandardMenu"
    RegistrationType="List"
    Sequence="1000"
    Title="Export to SoftArtisans OfficeWriter for Excel"
    ImageUrl="/_layouts/images/softartisanstutorial/actionicon_excel.gif">
    <UrlAction Url="/_layouts/SoftArtisansTutorial/ExcelWriter.aspx?ListId={ListId}"/>
</CustomAction>

WSS replaces the special {ListId} token with the actual id of the list. In the application page I tried to retrieve the list from which the action had originated as follows:

SPWeb web = SPContext.Current.Web;
string listId = Request.QueryString["ListId"];
SPList list = web.Lists[new Guid(listId)];

However, I found that SPContext.Current.Web always returned the root-level Web site, even if the action was activated from a child site. So I modified the URL of the action to include the URL of the site: Continue reading Targeting a SharePoint Site in a Custom Action

Amazingly Simple (and Fast) XML Formatting Trick

While pre-populating a SharePoint list instance with data, I find myself having to insert a large amount of unformatted data into an XML file in Visual Studio 2008. When formatted, the XML data consist of roughly 12000 lines. After I pasted the data into the file, Visual Studio took several agonizing minutes to format them into nicely human-readable form. Moreover, during this time, my computer’s CPU usage was also pegged at or near the maximum, rendering it practically unusable.

However, I found that if the XML data are already mostly formatted, then Visual Studio leaves them alone. So I tried to load the XML data into a text editor to see whether it could do the job faster. Unfortunately, my text editor isn’t capable of automatically formatting XML, and using regular expressions is too manual of a process.

Finally, I noticed that Internet Explorer loads and formats an XML file almost instantaneously. This suggests the simple trick of opening the XML file in IE, pasting the formatted XML content into a text editor to remove the extraneous hyphens, then pasting the results into Visual Studio. Doing so reduces the amount of time for the process from several minutes to just a few seconds.