Microsoft Vb.Net


The SharePoint Web Services

Windows SharePoint Services was being designed and developed during the time when Microsoft was beginning to heavily push Web services. It should be no surprise, then, to find out that you can get at the data in SharePoint through Web services. In fact, there’s not just one Web service involved; there are 16. Here’s a brief rundown of the Web services that a SharePoint server makes available out of the box:

  • http://server:5966/_vti_adm/Admin.asmx – Administrative methods such as creating and deleting sites
  • http://server/_vti_bin/Alerts.asmx – Methods for working with alerts
  • http://server/_vti_bin/DspSts.asmx – Methods for retrieving schemas and data
  • http://server/_vti_bin/DWS.asmx – Methods for working with Document Workspaces
  • http://server/_vti_bin/Forms.asmx – Methods for working with user interface forms
  • http://server/_vti_bin/Imaging.asmx – Methods for working with picture libraries
  • http://server/_vti_bin/Lists.asmx – Methods for working with lists
  • http://server/_vti_bin/Meetings.asmx – Methods for working with Meeting Workspaces
  • http://server/_vti_bin/Permissions.asmx – Methods for working with SharePoint Services security
  • http://server/_vti_bin/SiteData.asmx – Methods used by Windows SharePoint Portal Server
  • http://server/_vti_bin/Sites.asmx – Contains a single method to retrieve site templates
  • http://server/_vti_bin/UserGroup.asmx – Methods for working with users and groups
  • http://server/_vti_bin/versions.asmx – Methods for working with file versions
  • http://server/_vti_bin/Views.asmx – Methods for working with views of lists
  • http://server/_vti_bin/WebPartPages.asmx – Methods for working with Web Parts
  • http://server/_vti_bin/Webs.asmx – Methods for working with sites and subsites

To use any of these Web services, replace server with the name of your SharePoint server. Because they’re implemented using ASP.NET code, you can retrieve the matching WSDL file for any service by appending ?WSDL to the end of the URL. When you do so, you’ll discover that each one supports multiple methods, making this one of the richest sets of Web services of any current product. For full information on the available Web methods, download the SharePoint Products and Technologies 2003 SDK.

VB.NET to SharePoint

To see how this works in practice, I’ll build a simple client using Visual Basic .NET 2003. To hook the client application up to my SharePoint server, I selected Add Web Reference from the References node shortcut menu in Solution Explorer. Entering the URL for one of the SharePoint Web services shows me all of the methods supported by that service, as shown in Figure 1.

Setting a reference to a SharePoint Web service

Adding the reference makes all the methods of the Web service available to the Visual Basic .NET client application. Well, almost. SharePoint servers require authentication, and the proxy class that Visual Studio .NET creates automatically doesn’t pass any credentials to the Web service. Fortunately, that’s easy to fix by adding one line of code. Click the Show All Files button on the Solution Explorer toolbar and drill into the Web reference you just added until you find the Reference.vb file; this is the proxy class. Open this file and add a line to the constructor to pass the user’s default credentials:


Public Sub New()
    MyBase.New
    Me.Url = "http://seesaw/_vti_bin/Lists.asmx"
    Me.Credentials = System.Net.CredentialCache.DefaultCredentials
End Sub

Of course, the URL for your own SharePoint server will be different than mine. Now, I’m going to use the SharePoint Web service to do two things. First, I’ll retrieve information about all of the lists on the server to a DataGrid control. Second, when the user clicks on a row in the DataGrid, I’ll display the information from the corresponding list in a TextBox control. So, I’ve added a DataGrid named dgLists and a TextBox named txtData to the default form in my VB .NET application. When the form loads, this code will fill the DataGrid with information on the lists:


Private Sub Form1_Load(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles MyBase.Load
    ' Get the list of lists from the server
    Dim L As New WSS_Server.Lists
    ' The list is returned as an XmlNode object
    Dim n As XmlNode = L.GetListCollection
    ' Prepare objects to handle the data
    Dim xd As XmlDataDocument = New XmlDataDocument
    Dim ds As DataSet = xd.DataSet
    ds.ReadXmlSchema("Lists.xsd")
    ' Add an XML declaration to make the 
    ' XmlNode a valid XML document
    xd.LoadXml("<?xml version='1.0' ?>" & n.OuterXml)
    ' Display the result on the DataGrid
    dgLists.DataSource = ds
    dgLists.DataMember = "List"
End Sub

Most of the methods from the SharePoint Web services return XmlNode objects. An XmlNode object represents a node in an XML file, plus all of its children, but it isn’t a full XML document. You can use various methods to parse information from an XmlNode, but I’m going to turn it into a DataSet. The key to this is the XmlDataDocument object. The XmlDataDocument can hold any XML document, but it also exposes the document, via its DataSet property, as a DataSet. This gives me an easy way to map XML data to a DataGrid.

Before you load data from an XmlDataDocument into a DataSet, you need to specify the schema of the DataSet. This is especially useful if you want to display only some elements from the XML data. In this example, I’ve created a schema file that exposes all of the information in the XML as simple string columns:


<?xml version="1.0" standalone="yes"?>
<xs:schema id="Lists"
 targetNamespace="http://schemas.microsoft.com/sharepoint/soap/"
 xmlns:mstns="http://schemas.microsoft.com/sharepoint/soap/"
 xmlns="http://schemas.microsoft.com/sharepoint/soap/"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
 attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:element name="Lists" msdata:IsDataSet="true"
  msdata:EnforceConstraints="False">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="List">
          <xs:complexType>
            <xs:attribute name="DocTemplateUrl" form="unqualified"
             type="xs:string" />
            <xs:attribute name="DefaultViewUrl" form="unqualified"
             type="xs:string" />
            <xs:attribute name="ID" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Title" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Description" form="unqualified"
             type="xs:string" />
            <xs:attribute name="ImageUrl" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Name" form="unqualified"
             type="xs:string" />
            <xs:attribute name="BaseType" form="unqualified"
             type="xs:string" />
            <xs:attribute name="ServerTemplate" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Created" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Modified" form="unqualified"
             type="xs:string" />
            <xs:attribute name="LastDeleted" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Version" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Direction" form="unqualified"
             type="xs:string" />
            <xs:attribute name="ThumbnailSize" form="unqualified"
             type="xs:string" />
            <xs:attribute name="WebImageWidth" form="unqualified"
             type="xs:string" />
            <xs:attribute name="WebImageHeight" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Flags" form="unqualified"
             type="xs:string" />
            <xs:attribute name="ItemCount" form="unqualified"
             type="xs:string" />
            <xs:attribute name="AnonymousPermMask" form="unqualified"
             type="xs:string" />
            <xs:attribute name="RootFolder" form="unqualified"
             type="xs:string" />
            <xs:attribute name="ReadSecurity" form="unqualified"
             type="xs:string" />
            <xs:attribute name="WriteSecurity" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Author" form="unqualified"
             type="xs:string" />
            <xs:attribute name="EventSinkAssembly" form="unqualified"
             type="xs:string" />
            <xs:attribute name="EventSinkClass" form="unqualified"
             type="xs:string" />
            <xs:attribute name="EventSinkData" form="unqualified"
             type="xs:string" />
            <xs:attribute name="EmailInsertsFolder" form="unqualified"
             type="xs:string" />
            <xs:attribute name="AllowDeletion" form="unqualified"
             type="xs:string" />
            <xs:attribute name="AllowMultiResponses" form="unqualified"
             type="xs:string" />
            <xs:attribute name="EnableAttachments" form="unqualified"
             type="xs:string" />
            <xs:attribute name="EnableModeration" form="unqualified"
             type="xs:string" />
            <xs:attribute name="EnableVersioning" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Hidden" form="unqualified"
             type="xs:string" />
            <xs:attribute name="MultipleDataList" form="unqualified"
             type="xs:string" />
            <xs:attribute name="Ordered" form="unqualified"
             type="xs:string" />
            <xs:attribute name="ShowUser" form="unqualified"
             type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

The code loads this schema into the DataSet, loads the XmlNode into the XmlDataDocument (by prepending an XML declaration to turn it into a valid document), and then shows the data on the grid.

At this point, you’ll be able to inspect the data that SharePoint maintains on each list on the server. The key piece of information for the current example is the ID attribute, which is nothing more than a GUID that uniquely identifies each list. To retrieve the data in an individual list (such as Announcements, Events, or Tasks), you need to send that GUID back in the GetListItems method. The code does this when the user clicks on the grid (placing the code in the MouseUp event ensures that the selection happens before the code runs):


Private Sub dgLists_MouseUp(ByVal sender As Object, _
 ByVal e As System.Windows.Forms.MouseEventArgs) _
 Handles dgLists.MouseUp
    ' Connect to the server
    Dim L As New WSS_Server.Lists
    ' Arguments required by GetListItems
    Dim viewName As String
    Dim query As System.Xml.XmlNode
    Dim viewFields As System.Xml.XmlNode
    Dim rowLimit As String
    Dim queryOptions As System.Xml.XmlNode
    ' Get the items from the list specified
    ' by the current row in the DataGrid
    Dim n As XmlNode = L.GetListItems( _
     dgLists.Item(dgLists.CurrentCell.RowNumber, 2), _
     viewName, query, viewFields, _
     rowLimit, queryOptions)
    ' Get a transform ready
    Dim xslt As XslTransform = New XslTransform
    xslt.Load("Identity.xslt")
    ' Load the raw XML into an XML document
    Dim xd As XmlDocument = New XmlDocument
    xd.LoadXml("<?xml version='1.0' ?>" & n.OuterXml)
    ' Use the transform to prettify it 
    Dim ms As MemoryStream = New MemoryStream
    xslt.Transform(xd, Nothing, ms, Nothing)
    ' And display the results
    ms.Position = 0
    Dim sr As StreamReader = New StreamReader(ms)
    txtData.Text = sr.ReadToEnd()
End Sub

I can get the contents of the selected list by calling the GetListItems method and supplying the appropriate ID, which comes from the third column (that’s column number 2, because of zero-based counting) of the DataGrid in the same row as the current cell. The GetListItems method requires a bunch of arguments that can be used to narrow down the data that it returns. Although these arguments are not optional, they can be empty, so I’ve just supplied uninitialized variables for them.

Like GetListCollection, GetListItems returns an XmlNode object. The rest of the code in this method shows a useful little trick for “prettyprinting” XML data. If you apply a simple transform (which I’ve named Identity.xslt) to the original XML, it changes the indentation to make the XML more human-readable without changing any of the data. Here’s the contents of Identity.xslt:


<?xml version = "1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

That’s it! Figure 2 shows the end result of loading up the lists from my server and clicking on the Events list

Data from SharePoint in a VB .NET application

Where Do You Go From Here?

The usefulness of the SharePoint Web services is directly proportional to how much of your organization’s information ends up in the new version of SharePoint. But note that the very existence of these Web services is an argument in favor of using SharePoint rather than alternative stores. Anything you store in SharePoint isn’t locked up at all; it’s open to any client that can make a Web Services call (with the proper authentication, of course). It’s nice to see this movement towards open standards and away from closed APIs for things such as appointments, events, and tasks.

Advertisements

If you enjoyed this post, or this blog, please make a secure tax-deductable donation to the American Diabetes Association. Please read my personal story about life as a diabetic and donate today.

A while back, I posted a list of ASP.NET Interview Questions. Conventional wisdom was split, with about half the folks saying I was nuts and that it was a list of trivia. The others said basically “Ya, those are good. I’d probably have to look a few up.” To me, that’s the right response.

Certainly I wasn’t trying to boil all of .NET Software Development down to a few simple “trivia” questions. However, I WAS trying to get folks thinking. I believe that really good ASP.NET (and for that matter, WinForms) is a little [read: lot] more than just draging a control onto a designer and hoping for the best. A good race driver knows his car – what it can do and what it can’t.

So, here’s another list…a greatly expanded list, for your consumption (with attribution). I wrote this on a plane last week on the way from Boise to Portland. I tried to take into consideration the concerns that my lists contain unreasonable trivia. I tried to make a list that was organized by section. If you’ve never down ASP.NET, you obviously won’t know all the ASP.NET section. If you’re an indenpendant consultant, you may never come upon some of these concepts. However, ever question here has come up more than once in the last 4 years of my time at Corillian. So, knowing groking these questions may not make you a good or bad developer, but it WILL save you time when problems arise. 

What Great .NET Developers Ought To Know

Everyone who writes code

*       Describe the difference between a Thread and a Process?

*       What is a Windows Service and how does its lifecycle differ from a “standard” EXE?

*       What is the maximum amount of memory any single process on Windows can address? Is this different than the maximum virtual memory for the system? How would this affect a system design?

*       What is the difference between an EXE and a DLL?

*       What is strong-typing versus weak-typing? Which is preferred? Why?

*       Corillian’s product is a “Component Container.” Name at least 3 component containers that ship now with the Windows Server Family.

*       What is a PID? How is it useful when troubleshooting a system?

*       How many processes can listen on a single TCP/IP port?

*       What is the GAC? What problem does it solve?

Mid-Level .NET Developer

*       Describe the difference between Interface-oriented, Object-oriented and Aspect-oriented programming.

*       Describe what an Interface is and how it’s different from a Class.

*       What is Reflection?

*       What is the difference between XML Web Services using ASMX and .NET Remoting using SOAP?

*       Are the type system represented by XmlSchema and the CLS isomorphic?

*       Conceptually, what is the difference between early-binding and late-binding?

*       Is using Assembly.Load a static reference or dynamic reference?

*       When would using Assembly.LoadFrom or Assembly.LoadFile be appropriate?

*       What is an Asssembly Qualified Name? Is it a filename? How is it different?

*       Is this valid? Assembly.Load(“foo.dll”);

*       How is a strongly-named assembly different from one that isn’t strongly-named?

*       Can DateTimes be null?

*       What is the JIT? What is NGEN? What are limitations and benefits of each?

*       How does the generational garbage collector in the .NET CLR manage object lifetime? What is non-deterministic finalization?

*       What is the difference between Finalize() and Dispose()?

*       How is the using() pattern useful? What is IDisposable? How does it support deterministic finalization?

*       What does this useful command line do? tasklist /m “mscor*”

*       What is the difference between in-proc and out-of-proc?

*       What technology enables out-of-proc communication in .NET?

*       When you’re running a component within ASP.NET, what process is it running within on Windows XP? Windows 2000? Windows 2003?

Senior Developers/Architects

*       What’s wrong with a line like this? DateTime.Parse(myString);

*       What are PDBs? Where must they be located for debugging to work?

*       What is cyclomatic complexity and why is it important?

*       Write a standard lock() plus “double check” to create a critical section around a variable access.

*       What is FullTrust? Do GAC’ed assemblies have FullTrust?

*       What benefit does your code receive if you decorate it with attributes demanding specific Security permissions?

*       What does this do? gacutil /l | find /i “Corillian”

*       What does this do? sn -t foo.dll

*       What ports must be open for DCOM over a firewall? What is the purpose of Port 135?

*       Contrast OOP and SOA. What are tenets of each?

*       How does the XmlSerializer work? What ACL permissions does a process using it require?

*       Why is catch(Exception) almost always a bad idea?

*       What is the difference between Debug.Write and Trace.Write? When should each be used?

*       What is the difference between a Debug and Release build? Is there a significant speed difference? Why or why not?

*       Does JITting occur per-assembly or per-method? How does this affect the working set?

*       Contrast the use of an abstract base class against an interface?

*       What is the difference between a.Equals(b) and a == b?

*       In the context of a comparison, what is object identity versus object equivalence?

*       How would one do a deep copy in .NET?

*       Explain current thinking around IClonable.

*       What is boxing?

*       Is string a value type or a reference type?

*       What is the significance of the “PropertySpecified” pattern used by the XmlSerializer? What problem does it attempt to solve?

*       Why are out parameters a bad idea in .NET? Are they?

*       Can attributes be placed on specific parameters to a method? Why is this useful?

C# Component Developers

*       Juxtapose the use of override with new. What is shadowing?

*       Explain the use of virtual, sealed, override, and abstract.

*       Explain the importance and use of each component of this string: Foo.Bar, Version=2.0.205.0, Culture=neutral, PublicKeyToken=593777ae2d274679d

*       Explain the differences between public, protected, private and internal.

*       What benefit do you get from using a Primary Interop Assembly (PIA)?

*       By what mechanism does NUnit know what methods to test?

*       What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;}

*       What is the difference between typeof(foo) and myFoo.GetType()?

*       Explain what’s happening in the first constructor: public class c{ public c(string a) : this() {;}; public c() {;} } How is this construct useful?

*       What is this? Can this be used within a static method?

ASP.NET (UI) Developers

*       Describe how a browser-based Form POST becomes a Server-Side event like Button1_OnClick.

*       What is a PostBack?

*       What is ViewState? How is it encoded? Is it encrypted? Who uses ViewState?

*       What is the <machinekey> element and what two ASP.NET technologies is it used for?

*       What three Session State providers are available in ASP.NET 1.1? What are the pros and cons of each?

*       What is Web Gardening? How would using it affect a design?

*       Given one ASP.NET application, how many application objects does it have on a single proc box? A dual? A dual with Web Gardening enabled? How would this affect a design?

*       Are threads reused in ASP.NET between reqeusts? Does every HttpRequest get its own thread? Should you use Thread Local storage with ASP.NET?

*       Is the [ThreadStatic] attribute useful in ASP.NET? Are there side effects? Good or bad?

*       Give an example of how using an HttpHandler could simplify an existing design that serves Check Images from an .aspx page.

*       What kinds of events can an HttpModule subscribe to? What influence can they have on an implementation? What can be done without recompiling the ASP.NET Application?

*       Describe ways to present an arbitrary endpoint (URL) and route requests to that endpoint to ASP.NET.

*       Explain how cookies work. Give an example of Cookie abuse.

*       Explain the importance of HttpRequest.ValidateInput()?

*       What kind of data is passed via HTTP Headers?

*       Juxtapose the HTTP verbs GET and POST. What is HEAD?

*       Name and describe at least a half dozen HTTP Status Codes and what they express to the requesting client.

*       How does if-not-modified-since work? How can it be programmatically implemented with ASP.NET?
Explain <@OutputCache%> and the usage of VaryByParam, VaryByHeader.

*       How does VaryByCustom work?

*       How would one implement ASP.NET HTML output caching, caching outgoing versions of pages generated via all values of q= except where q=5 (as in http://localhost/page.aspx?q=5)?

Developers using XML

*       What is the purpose of XML Namespaces?

*       When is the DOM appropriate for use? When is it not? Are there size limitations?

*       What is the WS-I Basic Profile and why is it important?

*       Write a small XML document that uses a default namespace and a qualified (prefixed) namespace. Include elements from both namespace.

*       What is the one fundamental difference between Elements and Attributes?

*       What is the difference between Well-Formed XML and Valid XML?

*       How would you validate XML using .NET?

*       Why is this almost always a bad idea? When is it a good idea? myXmlDocument.SelectNodes(“//mynode”);

*       Describe the difference between pull-style parsers (XmlReader) and eventing-readers (Sax)

*       What is the difference between XPathDocument and XmlDocument? Describe situations where one should be used over the other.

*       What is the difference between an XML “Fragment” and an XML “Document.”

*       What does it meant to say “the canonical” form of XML?

*       Why is the XML InfoSet specification different from the Xml DOM? What does the InfoSet attempt to solve?

*       Contrast DTDs versus XSDs. What are their similarities and differences? Which is preferred and why?

*       Does System.Xml support DTDs? How?

*       Can any XML Schema be represented as an object graph? Vice versa?