|
Spaces home <blog lang="C#"/>ProfileFriendsBlogMore ![]() | ![]() |
<blog lang="C#"/>Microsoft technologies just make my job easier
|
|||||||||||||||||
|
|
6/27/2008 Free e-book: “Foundation of Programming”
He has released a free 79 page eBook for .NET developers covering design patterns, unit testing, mock objects, memory management, object relational mapping, and more. Hyper-V RTM released yesterday on Microsoft Downloads
Here are the downloads for the Remote Administration Tools: Technorati Tags: Windows Server 2008,Hyper-V 6/25/2008 Forms Authentication and Read/Anonymous Access
I tested the account using the default MasterPage in SharePoint and the account was fine when it had to display that page so I suspected some code I had attached to my .ascx control. The code block that executed was pretty simple which is below: String PageName = FileName(); If I commented out that code in the control the MasterPage loaded for the user account with “Read” access so I figured I needed to run the code “elevated” so I changed it slightly and surrounded it with SPSecurity.RunWithElevatedPrivileges(delegate() // code here }); This solved my issue with my .ascx control and the user with “Read” access no longer gets the “Error: Access Denied” page. A call to RunWithElevatedPrivileges switches both the User Identity and the Windows Identity, if you want to read more about this check out this great MSDN article on Security Programming in SharePoint 2007 by Ted Pattison.
Technorati Tags: Windows SharePoint Services v3.0, SharePoint Server 2007 6/20/2008 Getting Public Key Token of Assembly Within Visual Studio
I hated shelling out to the command prompt to do that, even though the new ctrl-shift right click “Copy as Path” in Windows Explorer made it easy to get the full path to the .dll to paste to the sn –Tp command.
Technorati Tags: Microsoft Office SharePoint Server 2007,Visual Studio 2008 6/19/2008 Compile a Web User Control (.ascx) w/HTML and Code Behind into a Single Assembly
There are a few things you’re going to need to get started:
Once you have all that configured (be patient)… Open Visual Studio 2008 and select File | New | Project… Select Visual C# | Web | ASP.NET Web Application Make sure you select the .NET 3.0 Framework as your target unless you have configured your SharePoint Server for .NET 3.5.
Enter a name for the Project “WebControls” and the Solution “WebPartDemo”, select the option to create a directory for the solution and click Ok. After your Web Application Project has been created right click the Project and select Properties | Signing Select the “Sign the assembly” checkbox and create a new strong name key with the same name as the Project and with no password. Close the Project properties and add a new Web User Control Named “WebUserControlSample” to the root of your project. Double click on the .ascx file to open it for edit and add the following: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControlSample.ascx.cs" Inherits="WebControls.WebUserControlSample" ClassName="WebControls.Markup.WebUserControlSample"%> <asp:Button ID="butDoWork" runat="server" Text="Say Hello" onclick="butDoWork_Click" /> <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /> <asp:Label ID="lblDisplay" runat="server" Font-Italic="true" Font-Size="Large" Text=""></asp:Label> The important portion of the code above is the ClassName entry, this is the namespace that will be used to reference your html markup. The code hehind for your Web User Control (.ascx.cs) can be something like this: using System; namespace WebControls { public partial class WebUserControlSample : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { } protected void butDoWork_Click(object sender, EventArgs e) { lblDisplay.Text = "Hello there " + txtName.Text; txtName.Text = string.Empty; } } }
You can delete the default.aspx page from your web site as we do not need that for this example. Now what you want to do is right click the Project in the Solution Explorer and select Unload Project, now right click the Project again and select Edit WebControls.csproj Immediately after the following lines: <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. <Target Name="BeforeBuild"> </Target> <Target Name="AfterBuild"> </Target> --> You want to paste the following build commands. Pay close attention to the following highlighted entries as they may differ depending on your Windows installation and the version of the SDK you have installed:
Save the file and then Right Click the Project and select “Reload Project” Right click the Project and select Build and you should see the following output: ------ Build started: Project: WebControls, Configuration: Debug Any CPU ------ Compile complete -- 0 errors, 0 warnings
If all has gone well then you should see the namespace WebControls.Markup in your assembly using Reflector for .NET:
You’ll also notice that your version number for the assembly is 0.0.0.0 which is not great, you can fix this by manually creating an App_Code folder in the WebControl project and moving your AssemblyInfo.cs file from the Properties folder into it (Make sure the build action on the file is set to compile) and you will have a version number stamped into your assembly on your next compile: Now if I add another Web Application to my solution, add a reference to my WebControls Project and edit the Default.aspx page to add a PlaceHolder control then go to the code behind and add the following code on the Page_Load: namespace WebControlTest { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Control myControl = Page.LoadControl(typeof (WebControls.Markup.WebUserControlSample), null); plhWebPart.Controls.Add(myControl); } } }
It will load my .ascx right out of the assembly! My next post will be to deploy this as a SharePoint Feature…stay tuned. I have posted the sample application outlined above here if you just want to jump right in.
|
|
|||||||||||||||
|
|