in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

October 2008 - Posts

  • Introducing Document Link Handler for MOSS 2007 Enterprise Search

    Since, I have started working with Enterprise Search, I have received many requests looking for ways to improve what we can do with documents.  The search results screen in MOSS, as flexible as it is, has always been lacking in features compared to search results of other ECM systems.  Common feature requests I have seen from clients include:  editing the document, viewing properties, linking to the folder or document library, linking to the site the document is in, zip and download, view version history, and more.  Today I am pleased to announce a simple solution which can handle many (but not all) of these common requests.

    So how do we accomplish this?  I decided to build a simple ASP.NET Handler (.ashx) file to parse the URL from a document on the search results page and redirect a user to the page that he or she wanted.  Links to the handler can easily be added by modifying the XSL of the CoreResultsWebPart.

    The code of the handler is pretty simple.  I take the URL passed via query string, split it to get the filename and the path and then I open an SPWeb object that references that site.  Once I have access to an SPFolder and SPFile object for the document, I can determine URLs for the document library and folder.  Take a look at the code if you are interested.  It might need some optimization, but it is a good starting point.

    Installation is simple.  Install the included .wsp file contained in the package folder.   The syntax is listed in the readme.txt file if you need it.  This will copy the DocumentLink.ashx file into your layouts folder.  To add the links to your search results, go to the results page of your search center and edit your CoreResultsWebPart.  In the Data View Properties, click on the XSL Editor button.  Replace the XSL in the window with the contents of the included SampleCoreResults.xslt file.  If you have already made customizations to this file or you only want certain links, you may copy just the lines you need from the readme.txt file.

    After you have changed the XSLT, apply the changes and execute a search query.  If you receive an error, then your XSL is probably malformed.  Open the XSL with Visual Studio if necessary to help find the error.  If all goes well, you should have search results that look like the one below.  Note, that these links will only show up for search results that are documents (IsDocument:1).  This works together in conjunction with my post earlier this week on adding an edit document link to search results.

    SearchResultsDocumentLinks

    The binaries and source code are available at CodePlex.  If you have any issues, please log them there on the Issue Tracker.  If you have any idea of other kinds of links we can provide off of a document library, please leave a comment.  This can also be used with the Wildcard Search web part.  Hopefully this will help many others out there deliver even richer search solutions to their customers.  Thanks.

    Document Link Handler for MOSS 2007 Enterprise Search

  • Adding an Edit Document Link to Enterprise Search Results

    I have seen a lot of requests to get more functionality on search results for documents lately.  People want to be able to edit documents, go to the folder the document is in, go to the site, view properties, etc.  I have solutions for all of those which I will be sharing in the next few days.  However, I wanted to share this one first, because it is easy to implement and does not require any custom code.  The goal is to get a link that will allow you to edit the document (instead of opening it in read only mode) directly from your search results. 

    As you will see the solution is actually quite simple.  Start by editing your results.aspx page and then edit the CoreResultsWebPart.  I have chosen to put my Edit Document link on the line following the title of the document.  In the Data View Properties of the web part, click XSL Editor.  Scroll down and look for the span with a class of srch-Title and insert the following block of code after the closing span and before the srch-Description.

        <div class="srch-Description">

            <a href="{$url}" onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','SharePoint.OpenDocuments.3','0

    ','SharePoint.OpenDocuments','','','','1','0','0','0x7fffffffffffffff')">Edit Document</a>

        </div>

    Save the XSL and click Apply.  You should now see an Edit Document link in your search results similar to the one below.

    SearchResultsEditDocumentLink

    Clicking on the Edit Document link will present the user with the Read Only / Edit menu just like they get when they are opening the document from a document library.

    SearchResultsEditDialog

    How did I figure this magic out?  It really was pretty simple, I just looked at a document library page and figured out what script method was being called.  If you spent money on a high dollar product just to get this feature, I am sorry.  Also remember that the Edit Document functionality only works in Internet Explorer.  Try it out and see if it works for you.  For other document functions such as Go to Folder, View Properties, and View Version History, take a look at my Document Link Handler for Enterprise Search.

  • Intro to SharePoint Development: How to Build and Deploy a Web Part

    UPDATE: Building Web Parts in SharePoint 2010

    I've recently helped out a number of developers new to SharePoint and I found that I have been often asked the same types of questions.  Most of those involve getting started and deployment, so today, I am beginning my series on getting started with SharePoint.  Most new developers find starting out pretty overwhelming, but once you get used to it, it does all make sense.  Hopefully this post will help the community and keep new SharePoint developers from getting scared off.  There have been a lot of posts on this topic I admit.  Since there are so many ways to do things, I wanted to write this post as a reference to new developers so they can see how I usually do things.  What I find is that most developers have no trouble getting a web part built, but when it comes time to deploy it, they are lost.  There are multiple ways to do this and not all of them are necessarily the right way.

    Environment

    Let's start by talking about your development environment.  In an ideal situation, you probably have your own virtual machine with Windows Server 2008 (or 2003), MOSS 2007, and Visual Studio 2008 installed.  However, maybe you don't have your own dedicated machine and you are going to be developing on a Windows XP machine, but deploying to a remote SharePoint server.  This is fine, but you are going to have to do some things a little differently.  First, you won't be able to install the Visual Studio Extensions for SharePoint.  You can live without this though because most people I have ran into say they don't use them.  Secondly, when it comes to deployment (which we'll talk about down below), you are going to have to copy your source files (either manually or via solution package) to the server.  You are also going to have to remote debug but fret not my post on it makes it easy.

    If you are developing on a desktop, another thing you will need to do is copy the SharePoint assemblies to your computer.  We can easily get these from a deployed SharePoint server.  However, this is a great time to take an aside and talk about some of the SharePoint directory structure.

    C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12

    All SharePoint developers have that file path permanently burned into their memory.  It is often referred to as the 12 hive.  A lot of things happen within this folder.  Subfolders here contain the SharePoint binaries, Master Pages, Application Pages, User Controls, Configuration Files, and Features.   Although SharePoint will let you customize any file in here, in general you don't want to make changes to any of these builtin files as it puts you in an unsupported scenario.  If you want to customize a master page or a style, it is typically best that you make a copy of what you want to customize and go from there.  Here is a quick list of some of the key folders and what they are used for.

    Folder Description
    CONFIG Contains partial trust configuration files
    ISAPI Binaries and SharePoint Web Services
    LOGS Error Logs (look here first when you get a strange error)
    TEMPLATE\CONTROLTEMPLATES User Controls
    TEMPLATE\FEATURES SharePoint Features (turns functionality on and off)
    TEMPLATE\IMAGES MainImages Folder
    TEMPLATE\LAYOUTS Pages and Styles
    TEMPLATE\SiteTemplates Definitions avaiable for deploying new sites
    TEMPLATE\THEMES Used to create custom themes in SharePoint
    TEMPLATE\XML Contains XSDs for any XML used with SharePoint

    Enough on that tangent, back to the assemblies that we need to copy.  Go to the above path and then go into the ISAPI folder.  Copy all of the DLLs from this folder onto your desktop machine in the corresponding folder name.  If you don't have that folder, create it.  You may also copy them to the Global Assembly Cache as well.  Once you are this point, you are ready to being building a web part.

    Coding the Web Part

    Start by creating a new class library project in Visual Studio.  There are packages and tools out there to automate some of these steps, but its best that you learn how to do it first by hand, so you know how to troubleshoot it should something go wrong.  Next, you will want to add (at a minimum) a reference to Microsoft.SharePoint.dll.  You can either grab it out of the GAC (will be listed under Windows SharePoint Services) or use the copy that you put in your ISAPI folder.  After you add it, make sure Copy Local is set to false on your reference.  You will also want to add System.Web since more than likely your web part will use an ASP.NET control in it.  You are now ready to create your web part.  To do this, create a new class and add a using statement for Microsoft.SharePoint.  In the past, you would derive your class from Microsoft.SharePoint.WebPart (and you still can), but now the more accepted way of doing things is to derive from the new System.Web.UI.WebParts.WebPart class.  The latter comes from ASP.NET 2.0 and can actually be deployed outside of SharePoint.  Here is what our class is going to look like.

    public class TestWebPart : System.Web.UI.WebControls.WebParts.WebPart

    {

        protected override void CreateChildControls()

        {

            base.CreateChildControls();

            Controls.Add(new Label(){Text = "My Test Web Part (Hello World)!"});

        }

    }

    The content of the class is simple.  We use the overridden CreateChildControls method to add ASP.NET controls to the page to do the rendering.  I simply call Control.Add and add a Label control with the text above.  Compile it and this web part is good to go, but there is the small matter of deploying it.  Compiling it yield you a DLL that has be deployed to SharePoint somehow.  SharePoint also needs to know where the web part is and how to reference it.  This is where the .webpart file comes in.

    Describing the WebPart

    The .webpart file is an XML file that tells SharePoint what to call your DLL and what assembly it is located in.  The easiest way to get started creating this file is to copy another one in SharePoint.  You can find plenty of examples by going to your web part gallery (Site Root -> Site Settings -> Web Parts).  this file simply describes the class and assembly of your web part as well as some default properties (i.e.: it sets a title and description).  You can also add your own properties by specifying a series of attributes on a property in your class.

    <webParts>

      <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">

        <metaData>

          <type name="MyWebPart.TestWebPart, MyWebPart, Version=1.0.0.0, Culture=neutral" />

          <importErrorMessage>Cannot import this Web Part.</importErrorMessage>

        </metaData>

        <data>

          <properties>

            <property name="Title" type="string">My Web Part</property>

            <property name="Description" type="string">A test web part.</property>

          </properties>

        </data>

      </webPart>

    Note: There is also a .dwp file, which comes from version 2, that can describe your webpart as well.  It still works but it is consider deprecated.

    Ghetto Deploying

    So far, we have built a web part and created a .webpart file so now it is time to deploy it.  We'll start by doing it the wrong way.  The first thing to know is that you can't just copy your DLL into the bin folder of your web application unless you change to full trust (not recommended) or specify Code Access Security.  Specifying CAS when first starting out will make even the most seasoned developers run for the hills.  So we are going to start by deploying to the GAC.  In almost all situations, I recommend against this, but for the sake of getting you started, it is ok.  You can then follow the Code Access Security post later on how to set up everything properly.

    Start by copying your compiled DLL into the Global Assembly Cache of your SharePoint server.  Keep in mind anytime you update this DLL, you will need to either reset IIS or recycle the application pool (otherwise the old version stays in memory).  The next thing you need to do is upload the .webpart file to the web part gallery of your site collection (again Site Root -> Site Settings -> Web Parts).  At this point, SharePoint will be able to recognize your web part and it can be added to a page.  If you click on the name of the web part in the gallery (in this case MyWebPart.webpart), it will display a preview of what it will look like on a page.  If everything is working correctly, you will see your web part, otherwise you will get an error.  At this point, you will have the following error.

    A web part on this web part page cannot be displayed or imported because it is not registered on this site as safe.

    This is because there is one more step to do.  Every web part or user control in SharePoint must be registered as safe to execute in the web.config.  To do this, find the SafeControls element near the top and add the following line (changing it with your fully qualified assembly path).

    <SafeControls>

      <SafeControl Assembly="MyWebPart, Version=1.0.0.0, Culture=neutral" Namespace="MyWebPart" TypeName="*" Safe="True" />

    </SafeControls>

    Now when you go back to the web part gallery, it should give you a preview of your web part.  If it doesn't check your assembly paths and make sure the file is deployed.  If you still have issues, check out this post on troubleshooting a web part.  Assuming you were able to get the web part to preview, you can also go add it to a page by going to any page, clicking Edit Page and then Add Web Part.  Scroll through the list until you find your web part and select it.  You should then see your web part on the page.

    Features

    So we talked about deploying a web part the wrong way.  Ok, it's not necessarily wrong, but it really adds a lot of extra work  This can be automated quite a bit by using features and solution packages.  Creating a feature, allows you to turn on and off customizations to SharePoint at the click of a button.  They can be used to deploy web parts, site definitions, workflows, document libraries, and plenty of other things.  In this case, we are building a feature to automate the deployment of our .webpart file.  You can also specify a class (Feature Receiver) that is executed when the feature is installed, activated, deactivated, or uninstalled.  A feature typically consists of two XML files.  The first file Feature.xml (must be named that), describes the feature and where its feature receiver is (if any). 

    <Feature

        xmlns="http://schemas.microsoft.com/sharepoint/"

        Id="{BDD425C3-CA50-4aee-9170-73954044D764}"

        Scope="Site"

        Hidden="False"

        Title="My Web Part"

        Description="My Test Web Part"

        >

      <ElementManifests>

        <ElementManifest Location="Elements.xml" />

      </ElementManifests>

    </Feature>

    The Id element contains a GUID.  Every feature needs a different one.  The Scope attribute specifies where to deploy the feature.  Possible values are Site, Web, WebApplication, and Farm.  I would go into the difference in scopes, because this post is already getting long enough.  You will most likely use Site or Web for most things you do (note on terms in API).  The ElementManifest element makes calls to additional XML files.  Typically the other file is called Elements.xml (although it can be called anything).

    The Elements.xml file can be used to deploy files, create document libraries, and many other functions.  I won't go into the whole detail of this file (it's in the SDK), but basically we are specifying that the MyWebPart.webpart file should be copied into the web part gallery.  I will tell you what a few things are though.  In this case the Module element says that we are going to deploy something into a list that is located at _catalogs/wp (this is the URL of the web part gallery).  How did I figure this out?  I looked at another example.  The File element deploys our MyWebPart.webpart file into the web part gallery.  I wont go into why you set the Type to GhostableInLibrary right now.  Just know this is the value you will pretty much always use.

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">

      <Module Name="WebPartPopulation" List="113" Url="_catalogs/wp" RootWebOnly="TRUE">

        <File Url="MyWebPart.webpart" Type="GhostableInLibrary" />

      </Module>

    </Elements>

    When the feature is activated, it will put the file in the web part gallery.  However, it won't remove the item from the web part gallery on deactivation.  It's pretty easy to do with code and I still plan to write a post on it.  For now, if you want the .webpart file gone you will have to delete it manually.  During the development cycle though, the only time it really is necessary to remove a .webpart file is if you have changed the namespace, class name, or public key token.  Here is what an elements file typically looks like for deploying a web part.

    So where do all these files go?  Let's talk about where you put them in your Visual Studio project first.  Typically, what you will do is create a folder structure in your project that matches the 12 hive.  So in this case, we will create a TEMPLATE\FEATURES folder.  We will then create a folder for our web part.  I am just calling it MyWebPart for now.  Here is what your solution will look like.  This folder is where you will put Feature.xml, Elements.xml, and MyWebPart.webpart.

    image

    To deploy the feature, copy it to the TEMPLATE\FEATURES folder in your 12 hive.  You then need to use stsadm command to install it.  The stsadm executable is located in the bin folder of the 12 hive.  You tend to use it a lot, so you might want to put it in your path environment variable.  Once you have located it, issue the following command.

    stsadm -o installfeature -name MyWebPart

    This makes the feature available to be activated.   To activate it, go to Site Collection Features (Site Root -> Site Settings -> Site Collection Features).  Find the feature in the list and click the Activate button.  This deploys the web part to the gallery and it can be added to pages as before.  If you ever want to remove the feature, use the uninstallfeature operation.  You will want to deactivate the feature first, otherwise you will have to use the -force parameter when using stsadm.

    Deploying Via Solution Package

    Features are nice, but we can take this one step further.  A solution package (.wsp file) allows you to package your entire solution into one .wsp file for deployment.  A .wsp file is simply a cab file with a manifest.xml file that tells SharePoint how to install the contents.  It will even deploy and install your feature for you, allowing you to skip the installation steps above.  Two files are required to build the .wsp file: cab.ddf and manifest.xml.  These files typically go into a folder called Solution in your Visual Studio project.  Cab.ddf tells the utility, makecab.exe, how to construct the .wsp file.   Below is an example DDF file.  You will need to set the CabinetNameTemplate to the filename you want for your wsp file.  You then specify a source and destination for each file you want copied.  In the example below, note that I am copying the DLL, Elements.xml, Feature.xml, and MyWebPart.webpart file. 

    ; ** MyWebPart.wsp **

    .OPTION EXPLICIT    ; Generate errors

    .Set CabinetNameTemplate=MyWebPart.wsp

    .set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory

    .Set CompressionType=MSZIP;** All files are compressed in cabinet files

    .Set UniqueFiles="ON"

    .Set Cabinet=on

    .Set DiskDirectory1=Package

     

    Solution\manifest.xml manifest.xml

     

    ; binary

    bin\debug\MyWebPart.dll MyWebPart.dll

     

    ; feature files

    TEMPLATE\FEATURES\MyWebPart\Elements.xml MyWebPart\Elements.xml

    TEMPLATE\FEATURES\MyWebPart\Feature.xml MyWebPart\Feature.xml

     

    ; web part files

    TEMPLATE\FEATURES\MyWebPart\MyWebPart.webpart FEATURES\MyWebPart\MyWebPart.webpart

    I won't go into manifest.xml again because I have already covered it once in this post.  It's this file that tells SharePoint how to deploy features, copy files, setup code access security, and add SafeControl entries to your web.config.  Once you create your solution files, build your project and then go to the command line.  The utility makecab.exe (should already be on your system) will actually create the wsp file.  In the command prompt, go to the root folder of your project and execute the following command.  It has to be executed from that folder because all of the paths are relative.

    makecab.exe /f solution\cab.ddf

    You can automate this step when you build, by creating a build action.

    If you are working on a remote server, you will need to copy the .wsp file onto the server. Add and deploy the solution with the following commands.

    stsadm -o addsolution -filename package\MyAssembly.wsp
    stsadm -o deploysolution -name MyAssembly.wsp -immediate -allContentUrls -allowGacDeployment
    stsadm -o execadmsvcjobs

    At this point if all goes well, your solution will be installed and deployed.  This eliminates all the steps of manually copying files into SharePoint.  You can then activate your feature like before (remember it installs the feature for you) and add your web part to a page.  If you make an update to your web part, deploying is easy, just use the upgradesolution stsadm command and it will update all the files in your solution.  Should you decide, you don't need the solution any more, use the retractsolution command.  After you issue either of those commands, you will need to follow it up with a execadmsvcjobs command as shown above.

    This may sound like a lot, but it really isn't bad.  It makes deployment really easy (especially to other servers).  Once you have your solution deployed and you want to make an update, you can also just directly copy out the binary to your server.  Not really a best practice, but it does speed things up quite a bit, since stsadm commands take a few seconds to run.

    Once you get the hang of it and are familiar with the deployment process, you really need to consider deploying your solution to the bin folder by deploying your solution under partial trust.  Remember right now, you are deploying to the GAC which is not ideal.  My post on Code Access Security walks you through the whole process and provides more details on how solution packages are built.

    User Controls

    By now, you may be thinking, I went through all of that and I still have to render stuff to the screen using straight code?  I hear you.  It's not any fun at all and can be a controversy among some SharePoint developers.  You do have a couple of alternatives, use the SmartPart or write your own.  I typically go with the latter.  This simple web part uses Page.LoadControl to load the .ascx file you specify.

    This is quickly becoming the longest post I have ever written.  I've attached the code I used in this post.  You can use it to verify that you put your own web part together correctly or you can use it as a starting point.  I hope this provides enough information for a new SharePoint developer to get started.  I certainly wish I had this much info condensed in one place when I was starting out.  You may also want to check out one of the many solution generating tools out there such as stsdev.  Please leave a comment if this helps or if you have any questions.  If I am missing anything or you think I need to add anything, also please let me know.

  • SharePint Tonight!

    Just a reminder about tonight's SharePint meetup at Crawpappy's.  Officially starts at 6, but we'll probably be there early.  This will be a great opportunity to talk to other members of the SharePoint community here in Tulsa.  I don't expect it to be as wild as Beer and Code, but it should still be a good time.  We'll see you there!

  • My 500th Post! Left Outer Joins with LINQ

    Today, this blog reaches an important milestone.  This is my 500th post.  It's just a number, but I thought I would use it as an opportunity to talk about the history of the blog and where it is heading.  On December 1st, 2004, I wrote my first .NET Tip of the Day about a great new method on primitives called TryParse in the upcoming release of ASP.NET 2.0.  This blog started out as a simple announcements list built on a SharePoint intranet site so that I could educate a team of developers on emerging technology (at the time it was .NET 2.0 and SQL Server 2005).  When I left that company, I wanted to keep the concept going so I exported all of the content and created a home-grown blogging engine at dotnettipoftheday.com.  Since then, I have been committed to giving back to the community by providing useful information that will help developers do their every day jobs.  My blog has always focused on how to do something not to give my opinion on something that I may or may not know anything about.  Admittedly, I haven't been able to keep up with the daily thing of posting (although I did a pretty good job back then), but I still make an effort to post a few things a week.

    Sometime in 2007, the term DotNetMafia was coined by the godfather himself Kyle Kelin.  Although the original idea for the mafia has evolved, it turned out to be a great way to get blogs from several of us onto one web site, dotnetmafia.com, which ran on DotNetNuke.  The site worked ok for us for a while, but it was lacking.  Later that year, I imported everything (well almost everything) into a new Community Server site where we are today.  In case you haven't seen the rest of the DotNetMafia bloggers, be sure and check out Tony Kilhoffer, James Ashley, Cory Robinson, and Kevin Williams.  They post on a variety of topics (including some outside of .NET).

    In nearly 4 years, I have brought you 500 posts.  112 of those posts have been on SharePoint.  Obviously you can see that I have shifted focus some.  The main reason is I blog about the things I am working with on a daily basis.  Right now, that has been SharePoint.  Although I still find time to blog about LINQ and other things from time to time.  Let's face it though, the SharePoint community needs all the help it can get.  Sites like SharePointBlogs.com (which this feed is syndicated too), have made a huge difference in helping people find the right information.  This is why I am committed to posting every nuance I find in the product, so that the next SharePoint developer coming along doesn't have to spend time trying to figure it out.

    I am pleased with how the site has grown to date.  In four years, we have gone from having a couple of internal users a month, to 200 - 300 unique visitors a day.  65% of that traffic each day comes from search.  The rest are from RSS subscriptions, referrals, and my CodePlex project.  For now, I plan to keep the content coming and encourage others to blog too.  I am committed to helping the community even more and hope to continue to grow the site and organize meetups.  I want to thank all of my friends, all two of my readers, Copy Source as HTML, and everyone that has left a comment.  I really appreciate it.

    Enough fluff for now though, let's get to some content.  I am going to step away from SharePoint for today's post and try to hit a broader audience talking about LINQ.

    Left Outer Joins with LINQ

    The topic I am talking about today is doing left outer joins with LINQ.  Doing a join in LINQ is pretty simple, however doing an outer join is a bit more tricky because the way you do it doesn't follow the way you would think of it using T-SQL.  The documentation on LINQ covers how to do this, but it doesn't do a great job explaining what you are doing and why.  For this example let's say we have two classes Product and Inventory.  The Product class has the following properties Name, ProductId, and Price.  The Inventory class has the properties ProductId and Count.  We want to get a list of all products and know how many we have in inventory but unfortunately the Inventory table isn't complete and it doesn't have data on all products.  We still want the product to return in a query though.

    First, for the purpose of example, I populate two lists.  This could just as easily come from an XML document or SQL.  In this case we have three products and two items containing inventory, but info is missing for one particular product.

    List<Product> productList = new List<Product>();

    productList.Add(new Product() { Name = "Product 1", Price = 10.99f, ProductId = 2 });

    productList.Add(new Product() { Name = "Product 2", Price = 1.99f, ProductId = 5 });

    productList.Add(new Product() { Name = "Product 3", Price = 3.99f, ProductId = 9 });

     

    List<Inventory> inventoryList = new List<Inventory>();

    inventoryList.Add(new Inventory() { ProductId = 2, Count = 54 });

    inventoryList.Add(new Inventory() { ProductId = 9, Count = 31 });

    If this was a T-SQL query it would probably look something like this:

    SELECT p.Name, i.Count FROM Products p LEFT OUTER JOIN Inventory i ON p.ProductId = i.ProductId

    In LINQ it is going to look a bit different though.

    var productInventory = from product in productList

                           join inventory in inventoryList

                           on product.ProductId equals inventory.ProductId into productInventoryGroup

                           from item in productInventoryGroup.DefaultIfEmpty(new Inventory() { ProductId = product.ProductId, Count = 0 })

                           select new

                           {

                               Name = product.Name,

                               ProductId = product.ProductId,

                               Count = item.Count

                           };

    The first thing we do is get a reference to an instance of the class (product) out of the productList with the from clause.  We then can join that to the inventoryList.  Remember in LINQ, you always have to have a reference to the class itself and not the collection when performing join and where operations, so we also get a reference to an instance of the class (inventory).  Once we have that we can use the on clause to relate the two lists using the ProductId field.  When doing a join you must use the keyword equals to relate the two collections.

    Here is where things really differ from what you may be used to.  The first thing that is different is that the result of the join is stored in a new variable using the into clause (in this case productInventoryGroup).  This is because in LINQ you use the DefaultIfEmpty() method to specify default values to return when there are no matching values on the right side of the join.  You can specify no parameter on this method to use the default when it is empty (usually null or 0 for an int), or you can pass a value for it to use.  In this case I created a new instance of Inventory and set Count to 0.  You then use another from clause to reference an item from that join.  Once you get to this point, it is just a matter of creating a new anonymous type, with product.Name and item.Count as values.  Remember, item in this case is of type Inventory and will contain the value from that list or the default if there is no match.

    You can then iterate through the result of the join like usual.

    foreach (var item in productInventory)

    {

        Console.WriteLine(string.Format("{0}: {1}<br />", item.Name, item.Count));

    }

    Which would return the following results.

    Product 1: 54
    Product 2: 0
    Product 3: 31
    

    It really isn't that bad, but it's just different than what you might be used to coming from the T-SQL world.  Hopefully, this explanation helps and will be of use to you. 

    Of course, don't forget that tomorrow night is the SharePint meetup at CrawPappy's at 6:00pm.  Thanks again.

  • Wildcard Search Version 1.0 Release 3

    Release 3 of the Wildcard Search Web Part is now available.  This minor update fixes a user reported issue where the scope property was not being used.  If you are new to the blog, the Wildcard Search web part allows a user to do partial word queries with Enterprise Search (i.e.: searching for red, returns results matching redmond, redding, and red).  Please continue to report bug on the CodePlex site.  Thanks again for everyone's input. 

    Also, don't forget the SharePint meetup, this Thursday at Crawpappy's at 6pm. 

  • Tulsa TechFest 2008 - Another Good Turnout

    Last week, I attended and spoke at Tulsa TechFest.  This year had a pretty good turnout thanks to David Walker and all of his volunteers..  With 17 tracks, it is no wonder, this event is reportedly one of the largest community organized tech events.  Even cooler that it's in Tulsa, OK.  Not only did it bring in people from Tulsa, but it also attracted people from Texas, Arkansas, Kansas, and Missouri (and probably other places - those are just the places that I met people from).  I decided to mix it up this year and check out some sessions other than the SharePoint ones.  I caught DotNetMafia's own Kyle Kelin's talk on jQuery along with Chris Patterson's talk on iPhone development.   I also caught Caleb Jenkin's talk on Silverlight with Visual Studio 2008.  I have to say Caleb has the best looking slide deck of any presenter I have ever seen.  No idea how he does it.

    Of course, I did hang out in the SharePoint room as much as possible.  I was the first speaker in the room and did my new session on Implementing Partial Trust in SharePoint (slides and code available at that link).  I also caught the SharePoint Cowboy's talk on High Performance SharePoint Development as well as Jim Hudson's and Richard Oltmann's talk on building fault tolerant SharePoint farms.

    All in all, it was a great conference and I had a good time as always.  The Beer and Code Meetup was immensely successful.  It had a good mix of attendees and speakers.  I am pretty sure Caleb taped everything, so no telling what we'll end up seeing on CommunityCast.tv.   I think it will be even bigger next year.  Maybe next year, we can make it official.  :)  Looking forward to next year.

    Also don't forget that this Thursday is the first SharePint Meetup at Crawpappy's.  We'll see you there.

  • Beer and Code Meetup was a Huge Success

    Last night after Tulsa TechFest, the DotNetMafia organized a meetup at Dirty's Tavern.  We had a pretty good turnout and ended up hanging out with people from the Tulsa branch of WAKA (World Adult Kickball Association).  Those people are a lot of fun.  Anyhow, I think everyone had a good time, and be looking for the DotNetMafia to do it again next year.   We'll be sure and get a food sponsor too.  Thanks for coming.

    Posted Oct 10 2008, 02:22 PM by CoreyRoth with no comments
    Filed under:
  • Don't forget Tulsa TechFest tomorrow!

    Just one last reminder that Tulsa TechFest is tomorrow and Friday.  There will be a lot of great speakers there and it should be a lot of fun.  I'll be giving a talk on the oh so exciting topic of Partial Trust and Code Access Security in SharePoint.  If you have ever wanted to deploy SharePoint code in the bin folder, this session is for you.

    Also don't forget the Beer and Code Meetup at Dirty's Tavern after the event!

  • Slides and Code Samples from Tulsa TechFest

    Today, at TechFest, I gave my talk on Implementing Partial Trust in SharePoint and I had a great time.  As promised, here are the slides and code samples.  If you have any more questions, feel free to leave me a comment.

  • Error: System.Security.SecurityException: Request for the permission of type 'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' failed

    Since I am talking about Implementing Partial Trust using Code Access Security this week, I thought I would talk about the following error when using a web part that doesn't have code access security set on it.

    System.Security.SecurityException: Request for the permission of type 'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' failed.

    This is the first error that someone taking their first dive into using partial trust with SharePoint might receive.  The reason I post this today is that a lot of times when someone runs into an issue with partial trust, someone will just say deploy your DLL to the GAC or change your trust level to full.  You don't need to this if you build your solution package (.wsp) to properly set permission levels.  If you run into this issue, check out my updated post on Configuring Code Access Security on a Web Part.  I wrote that post some time ago, but I have updated it as I have become more familiar with the process.

  • Updating Data with LINQ to SQL and GridViews when using a View

    Look! It's a post that isn't about SharePoint!  Today, I want to talk about some of the issues I have run into when trying to update data using a GridView when bound to a LinqDataSource.  This data source has an EnableUpdate property, but unfortunately there is more to it than just that.  It works pretty well, but there are a few things that you have to do otherwise you will get the following error.

    The Entity is not of the correct type.

    In today's example, I required a stored procedure to do the update because I was updating multiple tables behind the scenes.  Although LINQ can do this, the complexity of the update was simpler if I did it via stored procedure.  The data I am displaying in my GridView is based off a database view.  The first thing to do is drag the View into your LINQ to SQL class.  You need to make sure your view includes a primary key.  You then need to select the column and mark it as a Primary Key in the LINQ to SQL designer.  After you have your view defined, drag the stored procedure for your update into the designer.  Then on the properties of your view's data class, you can set a default method for Insert, Update, and Delete.  In this case I am only worrying about Update.  If you click on Update, it will bring up a Configure Behavior window which will allow give you two choices.  Use Runtime will dynamically generate the CRUD logic at runtime.  In our case though, we will choose Customize and then pick our stored procedure out of the list.

    After you have selected an update method, your LINQ to SQL class is good to go.  You are now ready to build your page.  Start by dragging a GridView and LinqDataSource onto your page.  On your LinqDataSource, set the EnableUpdate property to true.  Use the wizard to bind your LINQ to SQL database.   Pick the Entity named after your view but when configuring columns be sure and choose all columns (*).  If you select individual columns, it populates the Select property on the LinqDataSource and you will not be able to make updates.  At this point, you can bind your GridView to your LinqDataSource.  If you have followed all of the steps so far, the Enable Editing option should be available for you to check.  Check it and then the last thing you have to do is set the DataKeyNames property on the GridView.  Set it to the name of your primary key and you should be ready to make updates using  a GridView.

    I always find that I spend more time than I care to setting up GridViews.  Hopefully this will help if you ever need to use LINQ to SQL with a GridView.  Also don't forget Tulsa TechFest this week where I will be talking about Code Access Security in SharePoint.

  • Troubleshooting: Cannot import Web Part Error

    Let's face it, any new SharePoint developer is going to run into an error such as the one below when starting out.

    [WebPartPageUserException: Cannot import MyWebPart Web Part.]

      at Microsoft.SharePoint.WebPartPages.WebPartImporter.CreateWebPart(Boolean clearConnections)

      at Microsoft.SharePoint.WebPartPages.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, Uri webPartPageUri, SPWeb spWeb)

      at Microsoft.SharePoint.WebPartPages.SPWebPartManager.CompressWebPartNoSave(Boolean isClosed)

    I've found myself doing a LOT of troubleshooting of this for developers, so I decided it was time to post the steps in helping me resolve this issue.  This issue can be a number of things, but the first place I always check is the web part gallery.  If you can't view your web part in the gallery, chances are it's not going to work.  The first thing you need to do is verify that your web part is in fact in the list.  If it's not, you need to go install it (either manually or via solution package / feature).  If it is there, click on the link to the web part (i.e. MyWebPart.webpart), not the icon.  If you get an error like the one above, you definitely know something is wrong with the way the web part is installed on your server.

    The next thing you do is click the Edit icon (the one next to the webpart link).  From this page, you can make use of the View XML link to look at the .webpart file that is installed.  More than likely at this point, the assembly path is wrong to your DLL or the DLL is not installed on your system.  Confirm that the assembly path is correct given your DLL.  For example, if your DLL is strongly signed, make sure the public key token is listed on the assembly path.  If the DLL is not in the GAC, I recommend leaving off the PublicKeyToken becuase it will try and find that DLL in the global assembly cache which will give you an error saying that it cannot find the DLL.  Other issues I have ran into here is the version number.  Sometimes specify the version number as 1.0.* (for example).  This usually works but I have seen issues where it would not work unless you specified 1.0.0.0.  I am sure there is a reason for that, but I have no idea what it is.  Also remember, if you change your assembly path ever (i.e.: you change the PublicKeyToken or namespace) and redeploy, you need to delete the .webpart file out of the gallery.  You can do this manually or write code to do it with a feature receiver.

    If you have verified that your .webpart is correct, make sure that the DLL for you web part actually exists.  I know this sounds obvious, but lots of things can cause a solution package to install silently which means your DLL may not be present in the bin folder or the GAC when you think it should be.  Also remember that you probably need to reset IIS (or at least the application pool), if you have deployed a DLL to the GAC for SharePoint to pick it up.

    Of course, make sure you have a SafeControls entry in the web.config file for your assembly.  Although, this would probably result in a Cannot register this type as safe error.  This error can often times be quite frustrating because it will seem as if you have done all the steps to make sure it works and it just doesn't.  Don't forget to check the logs (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\logs) too.  This error will show up in there and there may be extra information in there to help you find your error.  Just keep at it and double check everything and usually you can find something.

  • Announcing Beer and Code Meetup after Tulsa TechFest

    I am excited to be speaking at Tulsa TechFest again this week.  As I mentioned in the past, the DotNetMafia will be doing a social event this Thursday night after TechFest hands out all those prizes around 6ish at Dirty's Tavern (325 E 2nd St).  Whether you are into SharePoint, Agile, .NET, PHP, Ruby, or whatever, this is a great chance to come by and mingle with other IT professionals.  I can't promise any free beer, but Dirty's Tavern has some of the cheapest beer in Tulsa.  Dirty‚Äôs Tavern is downtown.  Just minutes away from TechFest.  Also don't forget, that next week is the first SharePint meet up on October 16th.  Looking forward to seeing all two of my readers there!

    Posted Oct 06 2008, 11:05 AM by CoreyRoth with no comments
    Filed under:
  • Using SharePoint with Enterprise Library 4.0 under Partial trust

    Last year, I posted on how to use Enterprise Library 3.1 with SharePoint under partial trust.  It was incredibly difficult to get it to work with SharePoint when using partial trust because none of the DLLs had the AllowPartiallyTrustedCallers attribute on them.  This meant in order to get it to work, you had to add it to every AssemblyInfo.cs in the solution, sign it with your own strong name key, and then build and deploy it.  It was a lot of effort.

    Enterprise Library 4.0 has been out for a few months now, but I have yet to comment on it.   I am pleased to report that with this new release, none of this overhead is required.  All of the DLLs, have the AllowPartiallyTrustedCallers attribute which means all you have to do is reference the DLLs and assign appropriate permissions.  I recommend putting Enterprise Library DLLs in a solution package which will make setting permissions easier.  Last year's post should have most of the settings that you will need.

    If you got turned off trying to implement Enterprise Library in the past with SharePoint, you might give it another try.  With the APTCA change, it should make it much easier to use entlib for logging, exception handling, data access, or whatever.

2015 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems