Monday, August 2, 2010

Add Ajax 3.5 support in SharePoint

Here is an excellent link to add Ajax 3.5 support in SharePoint 2007. The author has detailedd the changes to the web.config

http://www.zimmergren.net/archive/2008/09/22/how-to-get-up-and-running-with-net-3-5-in-your-sharepoint-environment.aspx

Thursday, June 17, 2010

C# ,Impersonation for Cross Domain access

C# , Impersonation to write to a file outside the domain in a shared network location
I had a scene wherein I had to write to a file in a network location outside the domain using a domain Id.
The regular values for LogonUser() didn't work for me. So I changed the values for Cross Domain access.

This is the snippet that was different:
enum LogonType
{
Interactive = 2,
Network = 3,
Batch = 4,
Service = 5,
Unlock = 7,
NetworkClearText = 8,
NewCredentials = 9
}
enum LogonProvider
{
Default = 0,
WinNT35 = 1,
WinNT40 = 2,
WinNT50 = 3
}
LogonUser(
userName,
domain,
password,
(int)LogonType.NewCredentials,
(int)LogonProvider.WinNT50,

//LOGON32_LOGON_INTERACTIVE,
//LOGON32_PROVIDER_DEFAULT,
ref token) != 0)


Here is my complete code:
namespace Tools
{
#region Using directives.
// ----------------------------------------------------------------------

using System;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.ComponentModel;

// ----------------------------------------------------------------------
#endregion

/////////////////////////////////////////////////////////////////////////

///
/// Impersonation of a user. Allows to execute code under another
/// user context.
/// Please note that the account that instantiates the Impersonator class
/// needs to have the 'Act as part of operating system' privilege set.
///

///
/// This class is based on the information in the Microsoft knowledge base
/// article http://support.microsoft.com/default.aspx?scid=kb;en-us;Q306158
///
/// Encapsulate an instance into a using-directive like e.g.:
///
/// ...
/// using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
/// {
/// ...
/// [code that executes under the new context]
/// ...
/// }
/// ...
///
///
///

public class Impersonator :
IDisposable
{
#region Public methods.
// ------------------------------------------------------------------

///
/// Constructor. Starts the impersonation with the given credentials.
/// Please note that the account that instantiates the Impersonator class
/// needs to have the 'Act as part of operating system' privilege set.
///

/// The name of the user to act as.
/// The domain name of the user to act as.
/// The password of the user to act as.
public Impersonator(
string userName,
string domainName,
string password)
{
ImpersonateValidUser(userName, domainName, password);
}

// ------------------------------------------------------------------
#endregion

#region IDisposable member.
// ------------------------------------------------------------------

public void Dispose()
{
UndoImpersonation();
}

// ------------------------------------------------------------------
#endregion

#region P/Invoke.
// ------------------------------------------------------------------

[DllImport("advapi32.dll", SetLastError = true)]
private static extern int LogonUser(
string lpszUserName,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int DuplicateToken(
IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool RevertToSelf();

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool CloseHandle(
IntPtr handle);

private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_PROVIDER_DEFAULT = 0;
enum LogonType
{
Interactive = 2,
Network = 3,
Batch = 4,
Service = 5,
Unlock = 7,
NetworkClearText = 8,
NewCredentials = 9
}
enum LogonProvider
{
Default = 0,
WinNT35 = 1,
WinNT40 = 2,
WinNT50 = 3
}

// ------------------------------------------------------------------
#endregion

#region Private member.
// ------------------------------------------------------------------

///
/// Does the actual impersonation.
///

/// The name of the user to act as.
/// The domain name of the user to act as.
/// The password of the user to act as.
private void ImpersonateValidUser(
string userName,
string domain,
string password)
{


WindowsIdentity tempWindowsIdentity = null;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

try
{
if (RevertToSelf())
{
if (LogonUser(
userName,
domain,
password,
(int)LogonType.NewCredentials,
(int)LogonProvider.WinNT50,

//LOGON32_LOGON_INTERACTIVE,
//LOGON32_PROVIDER_DEFAULT,
ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
finally
{
if (token != IntPtr.Zero)
{
CloseHandle(token);
}
if (tokenDuplicate != IntPtr.Zero)
{
CloseHandle(tokenDuplicate);
}
}
}

///
/// Reverts the impersonation.
///

private void UndoImpersonation()
{
if (impersonationContext != null)
{ impersonationContext.Undo(); }
}

private WindowsImpersonationContext impersonationContext = null;

// ------------------------------------------------------------------
#endregion
}

/////////////////////////////////////////////////////////////////////////
}


For more details refer to :
http://www.cstruter.com/blog/270 which helped me .

Cheers!

Wednesday, June 16, 2010

Could not load type 'System.Web.UI.ScriptReferenceBase' from assembly 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf385

Ajax Error :
Could not load type 'System.Web.UI.ScriptReferenceBase' from assembly 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf385

I got this error while trying to use the Calendar control extender from the Ajax Control Toolkit for one of my textboxes.
First I got an error "sys.extended.ui is null or not an object" . To fix this, I changed the ScriptManager control to use the "ToolkitScriptManager" .
Once I did that I started getting this new error:Could not load type 'System.Web.UI.ScriptReferenceBase' from assembly 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf385
This got resolved once I installled .Net Framework 3.5 SP1.
That got my Ajax calendar control working like a charm ;)

Thursday, May 27, 2010

How to stop the !New icon from appearing in SharePoint Lists

I had this weird requirement from my client to remove the !New icon when a new document or item is added to a list.I followed these steps also outlined in the Microsoft support blog:

To stop the !New tag from appearing for new entries on your Windows SharePoint Services Web site, follow these steps, as appropriate for your version of SharePoint Services.
-Windows SharePoint Services 3.0 Web site
To stop the !New tag from appearing next to new entries on a Windows SharePoint Services 3.0 Web site, use the Stsadm.exe tool to change the "Days to Show New Icon" property to zero.

To do this, follow these steps:
1)Click Start, point to All Programs, point to Accessories, and then click Command Prompt.
2)Type the following commands, and then press ENTER after each command:
cd /d %programfiles%\Common Files\Microsoft Shared\Web Server Extensions\12\BIN
stsadm.exe -o setproperty -pn days-to-show-new-icon -pv 0 -url [Your Virtual Server's URL]

-Windows SharePoint Services Web site
To stop the !New tag from appearing next to new entries on a Windows SharePoint Services Web site, use the Stsadm.exe tool to change the "Days to Show New Icon" property to zero.

-To do this, follow these steps:
Click Start, point to All Programs, point to Accessories, and then click Command Prompt.
Type the following commands, and then press ENTER after each command:
cd /d %programfiles%\Common Files\Microsoft Shared\Web Server Extensions\60\BIN
stsadm.exe -o setproperty -pn days-to-show-new-icon -pv 0 -url [Your Virtual Server's URL]

-SharePoint Team Services Web site
To stop the !New tag from appearing next to new entries on a SharePoint Team Services Web site, use the Owsadm.exe tool to change the "New Item Display Cutoff" property to zero.

To do this, follow these steps:
Click Start, point to All Programs, point to Accessories, and then click Command Prompt.
Type the following commands, and then press ENTER after each command:
cd /d %programfiles%\Common Files\Microsoft Shared\Web Server Extensions\50\BIN
owsadm.exe -o setproperty -pn NewItemDisplayCutoff -pv 0 -p [Your Virtual Server's Port]

Tuesday, May 4, 2010

Setting up User Accounts in SharePoint 2007 :Useful Tips to share

  • A few years back,I was not sure of how User Accounts in SharePoint 2007 should be set up.
    Here is an useful extract I found to guide me:
    If you are installing SharePoint properly, you'll use the 'least privilege account principle'; this basically means that each distinct service inside the SharePoint farm will have its own domain user account. These accounts should have the minimum privileges that they need to perform their jobs. There is a great document which goes into detail on each different account (8+ accounts) here, however in summary, you should have the following accounts:
  • SQL Server Service Account: Account used by SQL to run all SQL services
  • Server Farm Account
  • SSP Service Account
  • Office SharePoint Server Search Account
  • Default Content Access Account
  • User Profile and Properties Content Access Account
  • Excel Services Unattended Account
  • One account per application pool: This is typically three accounts; SSPAdministration, MySite and your main 'Portal' or 'Intranet'.

For more information refer to this
http://technet.microsoft.com/en-us/library/cc263445.aspx

Tuesday, April 20, 2010

Infopath 2007 : Button Click event fired only once



I was trying to have multiple Rules on a Button on an InfoPath 2007 form. But I saw that only the rule on the first click is fired. All other rules don't get executed.

This is what I did to resolve the issue: Make that the checkbox that says "Stop Processing rules when this rule finishes " is checked for all the Rules to fire on Button click.

Make sure that on the Button properties --> Browser Forms you have the option of "Always" selected to send data to server on button click


This should make your button Rules fire on every click ! Happy Clicking!!

Infopath 2007 : Only first row of repeating table is committed to the database

This was a weird issue I came up with. Here is the background :

I was using Infopath 2007 to receive and submit data to a web service.

I had a repeating table bound to the receiving data and it displayed all the rows. But while submitting only the first row of the repeating table got committed.

Here is what I did to resolve the issue:

I had only one level in the receiving data schema a group called called TestData1 . Because of this the webservice was getting to the child level but not to the parent level. I added one more level in the webservice to change the schema to have TestData1 and then TestData1 again below it .

The web service was modified by adding a new class for the array which included one more level as below:


[WebMethod]

public TestDataArray getTestDataFilteredSorted(string USERNAME, string SORTCOLUMN, string SORTDIRECTION, string QUERY_NUM, string STUDY, string SITE, string QUERY_ID, string SUBJECT, string REVIEW_STATUS)
{
TestData filterTestData = new TestData();
filterTestData.QUERY_NUM = QUERY_NUM;

filterTestData.STUDY = STUDY;
filterTestData.SITE = SITE;
filterTestData.QUERY_ID = QUERY_ID;
filterTestData.SUBJECT = SUBJECT;
filterTestData.REVIEW_STATUS = REVIEW_STATUS;
TestDataArray filterTestDataArrayobj = new TestDataArray();
filterTestDataArrayobj.TestData1 = getTestDataFiltered2(USERNAME, filterTestData, SORTCOLUMN, SORTDIRECTION);

return filterTestDataArrayobj;
}
public class TestDataArray
{
public TestData[] TestData1;
}
Also note that while configuring the submit data connection make sure that you select the top group of TestData under field or group and Select "XML subtree" under include.
With all this configured you should be able to commit more than one row of the repeating table