Microsoft Technology, .Net, BizTalk, Sharepoint & etc.

Liedong(Ken) Zheng, Senior SharePoint Developer at SIMPLOT

Archive for December, 2008

Victoria.NET Back to Basics – Sessions on WCF, WPF and WF – Monday Dec 15th

Posted by ken zheng on December 10, 2008

case you missed this announcement from Mahesh… Cheers Dave

Monday 15th December 2008
5:30pm (Pizza and drinks served); 6pm (presentations start)
Microsoft Theatre, Level 5, 4 Freshwater Place, Southbank

Back to Basics – WCF, WPF and WF

Over two years ago Microsoft introduced .NET 3.0 and with it, it’s four pillars – Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), Windows Framework (WF) and Windows Cardspace. In “Back to Basics”, we present 3 detailed sessions on 3 of these topics – WCF, WPF and WF.

If you’ve never used these Technologies before or would like to re-acquaint yourself with them, this is the session to attend.

If you are planning on coming, please RSVP to help us organise drinks and catering.

To register, simply send an email to info@victoriadotnet.com.au. It is a FREE event and there is no charge for attendance but space is limited so you’ll need to let us know you’re coming so we can make sure there’s space. If you do not wish to receive event notification mails in the future, simply send a mail to info@victoriadotnet.com.au with the word Unsubscribe in the subject.

The user group has a Twitter account. You can follow the happenings in the group at http://twitter.com/vdnug/a>

The Victoria .NET Dev SIG usually meets on the second Tuesday of every month to discuss developer related .NET topics. Victoria .NET also runs an Office and SharePoint group called MOSSIG (that meets on the 4th Wednesday of every month) and a SQL SIG (that meets 12:30-2:00pm on the 2nd Monday of each month) to discuss topics related to SQL Server development and a new Silverlight Developer and Designer Network user group

Posted in Uncategorized | Leave a Comment »

Best Practise to Use SharePoint Site Object

Posted by ken zheng on December 10, 2008

                using (SPWeb userWeb = SPContext.Current.Web)
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite currentSite = new SPSite(userWeb.Site.ID))
                        {
                            using (SPWeb currentWeb = currentSite.AllWebs[userWeb.ID])
                            {
                                //allow unsafe updates
                                bool currentUnsafeSetting = currentWeb.AllowUnsafeUpdates;
                                currentWeb.AllowUnsafeUpdates = true;
                                returnValue = currentWeb.EnsureUser(loginName);
                                currentWeb.Update();
                                currentWeb.AllowUnsafeUpdates = currentUnsafeSetting;
                            }
                        }
                    });
                }

Posted in Sharepoint | Leave a Comment »

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack when use new spsite

Posted by ken zheng on December 10, 2008

You need to use

SPSite site = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
site = new SPSite(Configuration.MyPmpUrl);
});
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
return web;

To open your site
Below is the code to change pagelayout of default page

            string url = SPContext.Current.Web.Url + "/" + TextBox1.Text;
            string pageName = "default.aspx";
            string layoutName = "DefaultLayout.aspx";

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                using (SPSite site = new SPSite(url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        PublishingPage pubPage = null;
                        PageLayout pubLayout = null;
                        web.AllowUnsafeUpdates = true;

                        PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);

                        PublishingPageCollection pages = pubWeb.GetPublishingPages();

                        foreach (PublishingPage page in pages)
                        {
                            if (Equals(page.Name, pageName))
                            {
                                pubPage =  page;
                            }
                        }

                        PageLayout[] layouts = pubWeb.GetAvailablePageLayouts();

                        foreach (PageLayout layout in layouts)
                        {
                            if (Equals(layout.Name, layoutName))
                            {
                                pubLayout =  layout;
                            }
                        }

                        pubPage.CheckOut();

                        pubPage.Layout = pubLayout;
                        pubPage.Update();
                        pubPage.CheckIn("Page Modified For Layout Assimilation");
                        pubPage.ListItem.File.Publish(string.Empty);

                        if (pubPage.ListItem.ParentList.EnableModeration)
                        {
                            pubPage.ListItem.File.Approve(string.Empty);
                        }
                    }
                }

            });

Posted in Sharepoint | 1 Comment »

That assembly does not allow partially trusted callers.

Posted by ken zheng on December 9, 2008

If you get this message in your SharePoint, just open the web.config file and commented out trust level="WSS_Minimal" originUrl="" . That’s will be fine

Posted in Sharepoint | Tagged: | Leave a Comment »

Content Editor Webpart error “cannot retrieve properties at this time”.

Posted by ken zheng on December 9, 2008

Solution: Recycle your app pool or iis reset

Posted in Uncategorized | Leave a Comment »

“Project type is not supported by this installation” error in Visual Studio 2005

Posted by ken zheng on December 8, 2008

I couldn’t get a new installation of Visual Studio to open a web service project, and after a little digging I found a solution on Ira Miller’s blog that worked for me: install the Update to Support Web Application Projects for Visual Studio 2005 and the Visual Studio 2005 Web Application Projects add-in. After that, no problems opening the project, and the appropriate templates now appear in the File->New Project screen

Posted in .Net | Leave a Comment »

update the values of “Created By”, “Modified By” columns in SharePoint lists

Posted by ken zheng on December 7, 2008

/******** Code snippet for modifying the Created by and Modified by column values of a SharePoint List *******/
/******** Written in .Net Windows Based Application **********/

private void button3_Click(object sender, EventArgs e)
 {
            SPSite oSite = new SPSite("http://<SiteName>/");
            SPWeb oWeb = oSite.OpenWeb();
            SPList oList = oWeb.Lists["TestCustomList"];

            SPListItemCollection oListCollection = oList.Items;
            foreach (SPListItem oListItem in oListCollection)
            {
                SPFieldUserValue oUser = new SPFieldUserValue(oWeb, oWeb.CurrentUser.ID, oWeb.CurrentUser.LoginName);
   // or you can hard code the value like this,
 SPFieldUserValue oUser = new SPFieldUserValue(oWeb, 14, "Sowmyan Soman");
                oListItem["Author"] = oUser;//created by column value = "14;#Sowmyan Soman"
                oListItem["Editor"] = oUser;//modified by column value = "14;#Sowmyan Soman"
                oListItem.Update();
            }
            oWeb.Update();
         }

Posted in Sharepoint | 1 Comment »

Default Submit Button when Enter Press

Posted by ken zheng on December 7, 2008

TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+Button1.UniqueID+"').click();return false;}} else {return true}; ");

This line of code will cause that button Button1 will be “clicked” when visitors press Enter key and cursor is placed in TextBox1 textbox. On this way you can “connect” as many text boxes and buttons as you want.

If you remove document.getElementById(‘”+Button1.UniqueID+”‘).click(); the return key will not submit the form

Posted in .Net | 1 Comment »

Web.Config for Using Ajax Tool Kit

Posted by ken zheng on December 7, 2008

If you use VS 2008 to create a Ajax web site, that’s fine. As VS 2008 will create web.config for you. But if you still use VS 2005, you will have to edit the web.config by youself.

So I post a web.config for reference.

<?xml version="1.0"?>
<configuration>
	<configSections>
		<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
			<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
				<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
				<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
					<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
					<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
					<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
				</sectionGroup>
			</sectionGroup>
		</sectionGroup>
	</configSections>
	<system.web>
		<pages>
			<controls>
				<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			</controls>
		</pages>
		<!--
          Set compilation debug="true" to insert debugging
          symbols into the compiled page. Because this
          affects performance, set this value to true only
          during development.
    -->
		<compilation debug="true">
			<assemblies>
				<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
				<add assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>
				<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
				<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies>
		</compilation>
		<httpHandlers>
			<remove verb="*" path="*.asmx"/>
			<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
		</httpHandlers>
		<httpModules>
			<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
		</httpModules>
	</system.web>
	<runtime>
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<dependentAssembly>
				<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
				<bindingRedirect oldVersion="1.0.61025.0" newVersion="3.5.0.0"/>
			</dependentAssembly>
			<dependentAssembly>
				<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
				<bindingRedirect oldVersion="2.0.0.0" newVersion="3.5.0.0"/>
			</dependentAssembly>
		</assemblyBinding>
	</runtime>
	<system.web.extensions>
		<scripting>
			<webServices>
				<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
				<!--
      <jsonSerialization maxJsonLength="500">
        <converters>
          <add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>
        </converters>
      </jsonSerialization>
      -->
				<!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->
				<!--
        <authenticationService enabled="true" requireSSL = "true|false"/>
      -->
				<!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved
           and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
           writeAccessProperties attributes. -->
				<!--
      <profileService enabled="true"
                      readAccessProperties="propertyname1,propertyname2"
                      writeAccessProperties="propertyname1,propertyname2" />
      -->
			</webServices>
			<!--
      <scriptResourceHandler enableCompression="true" enableCaching="true" />
      -->
		</scripting>
	</system.web.extensions>
	<system.webServer>
		<validation validateIntegratedModeConfiguration="false"/>
		<modules>
			<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
		</modules>
		<handlers>
			<remove name="WebServiceHandlerFactory-Integrated"/>
			<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
		</handlers>
	</system.webServer>
</configuration>

Posted in AJAX | Tagged: | 2 Comments »

403 Forbidden When accessing SmartPart with AJAX

Posted by ken zheng on December 7, 2008

I found a solution to the 403 Forbidden (event id 1314) errors. Microsoft
Support couldn’t fix it but my two minute session with SysInternal’s FileMon
revealed the cause:
–> w3wp.exe attempts to access the bin/ directory of the
webapplication in context of the requesting (non-admin) user and fails with
an access denied.

SYMPTOMS
-User (non admin) wants to create a new list item (eg calenderitem, …) or
upload a file and sees a 403 Forbidden. The ‘new’ page is not show.
- User (non admin) wants to edit a list item (eg blogpost) and sees a 403
Forbidden. The ‘edit’ page is not shown.
- A Farm Administrator has NO issues with these ‘new’/'edit’ pages and is
able to edit and save content.

CAUSE
w3wp.exe (IIS worker process for ASP.NET) tries to look into the /bin
directory in the context of the requesting user account (impersonation), but
fails for non-admin users. The bin/ directory is not readable for normal
users but is readable for administrators.
I believe this is an ASP.NET 2.0 bug that is related to KB 928365. This
hotfix is removed when you install .NET 2.0 Service Pack 1. SP1 contains the
solution from KB 928365 and thus the problems continue.
I don’t think WSS3/MOSS or WSS3/MOSS Service Pack 1 causes this issue.

WORKAROUND/SOLUTION
Make sure your normal user accounts can access the /bin directory in the
webapplication:
- Open an Windows Explorer for the /bin directory of your webapplication and
display its properties.
- On the security tab, add the local server group “SERVER\Users” to the
list, (“SERVER\Users” usually contains “DOMAIN\Users”) and select the
following rights for “SERVER\Users”: “Read & Execute”, “List Folder
Contents”, “Read”. Click OK to apply the new settings.

The 403’s should be gone.
Do this for every webapplication in your farm.

Posted in Sharepoint | Tagged: | Leave a Comment »