Byte My Data

A personal collection of advice and solutions I've used.

About the author

Something about the author

Get the URI of the root SharePoint site in a web application and instantiate a SPWeb object for it

Here's the code from the previous post,

 

// The SharePoint portal URI of the root site.
var portalUrl = new Uri(Request.Url.GetLeftPart(UriPartial.Authority));
// The SharePoint sub site.
var portalSite = Request.QueryString["site"];
.
.
.
// Instantiate the SharePoint website object located at [portalUrl]/[portalSite].
SPWeb mySite = SPWebApplication.Lookup(portalUrl).Sites[portalSite].AllWebs[portalSite];

Categories: .NET | ASP.NET | C# | SharePoint
Permalink | Comments (1) | Post RSSRSS comment feed

Object reference not set to an instance of an object when instantiating a SharePoint SPWeb object in a web app

This is the reason we ran into this issue, it was simple and had nothing to do with the code, it's one of those "things to keep in mind" LOL.

We've got a web app in a virtual directory within the SharePoint site and a team member was trying to access it using the domain that was pointing to the site (http://portal.mydomain.com/myapp) and this was throwing the exception.

When the app was accessed via the server name, it worked (http://SERVERNAME/myapp).

The reason was that the SharePoint site was created as SERVERNAME and not portal.mydomain.com and the app was looking up the SharePoint site based on the current url (portal.mydomain.com) which did not exist.

 

// The SharePoint portal URI of the root site.
var portalUrl = new Uri(Request.Url.GetLeftPart(UriPartial.Authority));
// The SharePoint sub site.
var portalSite = Request.QueryString["site"];
.
.
.
// Instantiate the SharePoint website object located at [portalUrl]/[portalSite].
SPWeb mySite = SPWebApplication.Lookup(portalUrl).Sites[portalSite].AllWebs[portalSite];

 


Permalink | Comments (3) | Post RSSRSS comment feed

How to create a unique identifier GUID

// The identifier generated is alpha-numeric in the format:
// 00000000-0000-0000-0000-000000000000
// Blank identifier.
System.Guid blankGuid = System.Guid.Empty();
// New unique identifier.
System.Guid myGuid = System.Guid.NewGuid();

Categories: C# | .NET
Permalink | Comments (0) | Post RSSRSS comment feed

Check for IsPostBack using javascript

I found this in an MSDN post.

You can't do it directly but if you add the following to the server side Page_Load in the code behind, you can check for the javascript variable on the client side.

server side:

        protected void Page_Load(object sender, EventArgs e)
        {
            // NOTE: the following uses an overload of RegisterClientScriptBlock()
            // that will surround our string with the needed script tags .
            ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", string.Format("var isPostBack = {0};", (IsPostBack ? "true" : "false")), true);
        }

client side:

<head>
...
   
...
</head>
<body onload="doSomething();">

Categories: ASP.NET | C# | Javascript
Permalink | Comments (2) | Post RSSRSS comment feed

Using SubSonic 3.0 in a VB.NET web application

SubSonic is A Super High-fidelity Batman Utility Belt that works up your Data Access (using Linq in 3.0), throws in some much-needed utility functions, and generally speeds along your dev cycle. (http://subsonicproject.com)

The SubSonic code is in C# and the examples say to drag the folder containing the DAL templates into your project. Now that's all fine and dandy for an example and if the project is in C# but what if it's in VB or you want to separate the DAL? The simple thing to do is:

  1. Add a new C# project to your existing solution.
  2. Place all the files in the Subsonic DAL template folder into the root project folder - don't put them in a sub-folder.
  3. Include only the *.ttinclude files into the project (leave the *.tt files excluded).
  4. Reference the SubSonic.Core.dll.
  5. Make your changes to the Web.config and Settings.ttinclude files.
  6. Delete the Default.aspx page (I didn't need it).
  7. Now include the *.tt files into the project and that should create the class files.
  8. Build this project only.
  9. Add a reference to this project in you VB.NET project.

And that's it, you're all set.


Tags:
Categories: ASP.NET | C# | Databases | VB.NET
Permalink | Comments (1) | Post RSSRSS comment feed