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

Crystal Report Viewer parameter input form keeps appearing on refresh when trying to use the toolbar

We have various document libraries in a sharepoint portal that contains our report rpt files and we use an external web app to view reports in the libraries.
 
The app consists of one Default.aspx page containing the CrystalReportViewer control and a label control.
 
The parameters are set in the rpt file and if any and the report viewer will prompt the user for them.
 
The problem is that whenever we try to navigate, export, print, basically use any function of the toolbar, the page of course refreshes and goes back to the parameter input form.
To fix it I had to do a few things.
  1. Saved the report document into a session variable and load it back in after postback but;
  2. Disabled the parameter prompt on postbacks.
  3. To fix the paging issue, I had to move the code from the Page_Load to the Page_Init (found the solution here: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=2709)
        protected void Page_Init(object sender, EventArgs e)
        {
            // Exit if the session variable containing the report document is null (or does not exist).
            if (null == Session["ReportDocument"]) return;

            // Set the Crystal Report Viewer source to our previously saved report document.
            var crdoc = (ReportDocument)Session["ReportDocument"];
            crViewer.ReportSource = crdoc;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // We only want to see the parameter form on the initial page load.
            crViewer.EnableParameterPrompt = !IsPostBack;

            // We only want to continue on postback and if the session variable containing 
            // the report document is not null (exists).
            if (IsPostBack && null != Session["ReportDocument"]) return;

            // Load the rpt file from the sharepoint library and display it in the viewer.
            // The report document is saved into the session variable in this method.
            DisplayReport();
        }

Permalink | Comments (4) | 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

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

For some reason I started running into this error when I was trying to deploy a sharepoint webpart from visual studio. It just started happening out of the blue.

First I just rebooted the server and it deployed fine but when I made some changes to the code and tried to deploy it, the error came up again.

I found the answer here:http://social.msdn.microsoft.com/Forums/en/vsreportcontrols/thread/26accc30-9cfb-4d86-9c27-780f51929ecb

I had to add the ASP.NET account to each section.

  • Run dcomcnfg to bring up Component Services window.
  • Expand Component Services > Computers > My Computer > DCOM Config
  • Scroll down list, right click Windows Management and Instrumentation, and select properties.  A 5 tabbed window named Windows Management and Instrumentation Properties should be displayed.
  • Select the Security tab, then select radio buttons which activate the Edit... buttons.
  • Select the appropriate location and add the ASPNET account.
  • Reboot.
  •  


    Permalink | Comments (2) | Post RSSRSS comment feed

    Add a new line in a label control within a sharepoint webpart edit/config form

    I was creating a webpart and creating some custom edit values and I wanted to add a label for the dropdownlists I was adding.

    Adding the label before the dropdown was fine but I needed the dropdown on the next line. I tried using "<br/>" but that gave me an error when I tried to build it in visual studio.

    I tried System.Environment.NewLine but that didn't work and I tried "\n" with no luck and "\r\n" gave me an error when I tried to edit the webpart settings.

    I noticed that it seemed like the webpart error was being thrown whenever I had a slash combination in the text (i.e. "\r", "</...>") so I tried the non-html compliant "<br>" tag and that finally worked.

    Using this worked as well.

     myControl.Add(new LiteralControl("

    "))

    Categories: ASP.NET | SharePoint
    Permalink | Comments (2) | Post RSSRSS comment feed

    RadControls - javascript error telerik is undefined

    I'm using RadControls for AJAX Q1 2008 I got the 'telerik is undefined' javascript error on both IE7 and Firefox 3.5.3 and the way I fixed it was to replace the RadScriptManager I was using with the regular .NET ScriptManager.

    Apparently, in the latest versions of the RadScriptManager control (Q3 2009), there is an OutputCompression and setting it to false will solve the problem as well but I'm not able to test it; something you can try.


    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

    Datasets in App_Code folder not being recognized after converting website project to web application.

    I was getting "Type 'TeamStats.TeamStatsDataTable' is not defined" and "Type 'TeamStatsTableAdapters.TeamStatsTableAdapter' is not defined" errors in my code.

    In my case it was the datasets but it could be for any code file you have stored in the App_Code folder in a website project.

    The App_Code folder isn't recognized as an ASP.NET folder in a web application project so you'll have to move the files from there into another location in your project.

    I moved the *.xsd and *.xss dataset files into a new folder and then right-clicked on the folder in Visual Studio and selected "Include In Project" which generated the *.Designer.* files for the datasets.


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