Posted by ken zheng on October 21, 2009
To add new user to the web is very common process but fails most time. See http://sharepointsearch.com/cs/blogs/sharepointblogs/archive/2007/12/20/using-spweb-ensureuser-loginname-to-add-a-new-spuser-to-a-web.aspx for more details.
The way to solve this problem is to user SPWeb.EnsureUser(loginName).The description in the SDK for EnsureUser is:
“Checks whether the specified login name belongs to a valid user of the Web site, and if the login name does not already exist, adds it to the Web site.” Which happens to be exactly what we want!
Now we can finish our code:
SPUser newUser = newWeb.EnsureUser(@"domain\username");
newWeb.AllowUnsafeUpdates = true;
// Create the new roleassignment that we want to add to the collection of roleassignments of the new web
SPRoleAssignment roleAssignment = new SPRoleAssignment(newUser);
SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
// Add the binding to the correct roledefinition to the roleassignment
// This can also be Contribute for contributor rights.
// Keep in mind that in sites in other languages this needs to be translated
roleDefBindings.Add(roleDefinitions["Read"]);
roleAssignments.Add(roleAssignment);
newWeb.AllowUnsafeUpdates = false;
newWeb.Dispose();
portalSite.Dispose();
Posted in Sharepoint | Tagged: Sharepoint, SPUser | Leave a Comment »
Posted by ken zheng on June 30, 2009
The code below is to use the global navigation bar and Inherit site master page from parent of this site
projectDetailWeb.Navigation.UseShared = true;
//set masterpage to Inherit site master page from parent of this site
projectDetailWeb.MasterUrl = projectDetailWeb.ParentWeb.MasterUrl;
projectDetailWeb.AllProperties["__InheritsMasterUrl"] = "True";
projectDetailWeb.CustomMasterUrl = projectDetailWeb.ParentWeb.CustomMasterUrl;
projectDetailWeb.AllProperties["__InheritsCustomMasterUrl"] = "True";
projectDetailWeb.AlternateCssUrl = projectDetailWeb.ParentWeb.AlternateCssUrl;
projectDetailWeb.AllProperties["__InheritsAlternateCssUrl"] = "True";
projectDetailWeb.Update();
Posted in Sharepoint | Tagged: Sharepoint, Site Master Page | 1 Comment »
Posted by ken zheng on June 16, 2009
Sometime the list web part doesn’t show the change view dropdown list, I found this soltuion by just removing the web part and re-add it from the closed web part in Advanced Web Part gallery and options. It works.
See the blog for more details
Posted in Sharepoint | Tagged: Sharepoint | Leave a Comment »
Posted by ken zheng on May 13, 2009
I got the below errors on the SharePoint Server in every minute. This looked like the incoming email drop folder was not setup properly in the Central Admin Console. We checked and sure enough the drop folder was empty. Setting that value fixed this error. To set the drop folder
Central Admin-> Incoming email settings
Click Advanced and type in the email drop folder.
Event Type: Error
Event Source: Windows SharePoint Services 3
Event Category: E-Mail
Event ID: 6872
Date: 5/25/2008
Time: 9:16:22 AM
User: N/A
Computer:
Description:A critical error occurred while processing the incoming e-mail drop folder.
The error was: Value cannot be null.
Parameter name: path.
For more information, see Help and Support Center at
Event Type: Error
Event Source: Windows SharePoint Services 3
Event Category: Timer
Event ID: 6398
Date: 5/25/2008
Time: 9:16:22 AM
User: N/A
Computer:
Description:The Execute method of job definition Microsoft.SharePoint.Administration.SPIncomingEmailJobDefinition (ID 15cb7ef8-cfa0-4d8e-9892-4deb1c69d0f7) threw an exception. More information is included below.
Value cannot be null.
Parameter name: path
Posted in Sharepoint | Tagged: Sharepoint | 1 Comment »
Posted by ken zheng on May 8, 2009
To display sharepoint friend error page, you need to change the web.config file and
make sure:
<customErrors mode=”On” />
and
<SafeMode MaxControls=”200″ CallStack=”false” …>
And if you get an error in your custom code that you want to handle using the “out of the box” SharePoint mechanisms, use the following lines of code…
Make sure you only call the ‘TransferToErrorPage’ method if you are handling errors in the a user interface (web part, page, etc…). Don’t call that method in a workflow, event handler, etc…
//This will write to SharePoint’s log files.
Microsoft.Office.Server.Diagnostics.PortalLog.LogString(“Your Error Message”);
//This will redirect you to SharePoint’s error page.
Microsoft.SharePoint.Utilities.SPUtility.TransferToErrorPage(“Message to display on the page”);
Posted in Sharepoint | Tagged: Sharepoint | Leave a Comment »
Posted by ken zheng on April 28, 2009
Wonder if anyone across this problem and have any idea. We have created a couple attributes in AD but they are not showed in Data source field to map in Edit User Profile Property after I did full import.
I finally figure it out, first go to Search Setting and to see if it is in the Managed Properties. Also check to see if there are duplicated property in user profile. You may have to delete existing one and recreate in User Profile
Posted in Sharepoint | Tagged: Sharepoint | Leave a Comment »
Posted by ken zheng on April 28, 2009
1) Go to Internet Explorer options.
2) Add the SharePoint site to the Trusted Location.
3) Go to Custom Level and scroll to last option and change it Automatic logon with current username and password.
4) You may also do it for Intranet Zone if required.
Posted in Sharepoint | Tagged: Sharepoint | Leave a Comment »
Posted by ken zheng on April 24, 2009
Atidan Document Explorer for SharePoint is a tree-view control that displays your Microsoft® SharePoint site’s document library structure and content in the familiar explorer type view.
Document libraries and associated contents are presented in an intuitive, collapsible, hierarchical format.
The is a great control, but the problem is it displays all document libraries under site. Because it is using User Control and no property to set. So I decide to extend the web part which allow user specify the document library.

You can download the source from here.
Posted in Sharepoint | Tagged: Sharepoint, webpart | Leave a Comment »
Posted by ken zheng on April 23, 2009
Check for [XmlRoot(Namespace = "ClassNameSpace")] in top of ur class definition, remove this statement and deploy again and see what happens. I got the same error mentioned and when I remove the above statement it works fine with me…
Posted in Sharepoint | Tagged: Sharepoint | 2 Comments »
Posted by ken zheng on April 3, 2009
Problem:
When running SPListItem.Update commands inside SPSecurity.RunWithElevatedPrivileges block you get error: Operation is not valid due to the current state of the object.
Thoughts:
I will call this a workaround since I don’t know why it works and is this really how it should be done.
Workaround:
You must not call the SPListItem.Update inside the RunWithElevatedPrivileges block. Instead you should only instantiate the SPSite or SPWeb there and call Update afterwards, like this:
private static string CreateSiteLink(SPWeb newSite, int groupId, string text)
{
string retVal = "";
SPWeb elevatedRootWeb = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(newSite.Site.ID))
{
elevatedRootWeb = elevatedSite.RootWeb;
}
});
SPList userInformation = elevatedRootWeb.Lists["User Information List"];
if (userInformation != null)
{
try
{
elevatedRootWeb.AllowUnsafeUpdates = true;
SPListItem item = userInformation.GetItemById(groupId);
if (item["Notes"] != null)
{
item["Notes"] = string.Format(text, newSite.ServerRelativeUrl, newSite.Name);
item.Update();
}
elevatedRootWeb.AllowUnsafeUpdates = false;
}
catch (Exception e)
{
retVal = " Site link for group " + groupId + " couldn't be created. (" + e.Message + ")";
}
}
else
{
retVal = " Site link for group " + groupId + " couldn't be created. (User Information List not found)";
}
finally
{
elevatedRootWeb.Dispose();
}
return retVal;
}
Posted in Sharepoint | Tagged: Sharepoint | 3 Comments »