Problem Description How to access the current SharePoint Users information. Solution Description Code Snippet for the MS-Sharepoint 2007 Code Snippet ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------ * * * General Code Snippet for the MS-Sharepoint 2007 * * * ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------ 1. Access SharePoint User Information ------------------------------------------------------------------------------------------------------------------------------ Description : This piece of code shows you how to access the current SharePoint Users information. Code: SPWeb thisWeb = SPControl.GetContextWeb(Context); SPUser currentUser = thisWeb.CurrentUser; ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------ 2. Determine if there are files and folders ------------------------------------------------------------------------------------------------------------------------------ Description : This example shows you how to determine whether there are files or folders in a SharePoint asset. This is an asset existence method and can be used to prevent the overriding of other content. Code: public static bool FolderContainsAssets(SPFolder folder, string fileName) { foreach (SPFile file in folder.Files) { if (string.Compare(fileName, file1.Name, true) == 0) { return true; } } return false; } public static bool FolderContainsOtherFolders(SPFolder folder, string folderName) { foreach (SPFolder folder in folder.SubFolders) { if (string.Compare(folderName, folder1.Name, true) == 0) { return true; } } return false; } ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------ 3. Display A Users Role Collection ------------------------------------------------------------------------------------------------------------------------------ Description : This snippet shows you how to display a particular users set of roles based on the user name parameter. Code: int Roles = Site.Roles.Count; for (int iRole = 0; iRole < nRoles; iRole++) { SPRole Role = Site.Roles[iRole]; UInt32 hexPermMask = (UInt32)Role.PermissionMask; lstSiteRoles.Items.Add( "Role" + iRole.ToString() + ": " + Role.Name + " [Type:" + Role.Type.ToString() + " PermMask:" + hexPermMask.ToString("X4") + " " + Role.PermissionMask.ToString() + " " + "]" ); } ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------ 4. Get a SharePoint Field By Its Name ------------------------------------------------------------------------------------------------------------------------------ Description : This piece of code demonstrates how you could return a SharePoint field by passing in the name parameter. Code: private SPField GetSharePointFieldByName(string name) { SPField field = null; try { return this._list.Fields[name]; } catch { } foreach (SPField field in this._list.Fields) { if (field.InternalName.Trim().ToLower() == name.Trim().ToLower()) { return field; } } return field; } ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------ 5. Get List View Items ------------------------------------------------------------------------------------------------------------------------------ Description : This code sample provides method that will get the list items that are present in a SharePoint view. Code: protected SPListItemCollection GetListViewItems() { return this.GetListViewItems(string.Empty); } protected SPListItemCollection GetListViewItems(string viewFields) { try { SPQuery query = new SPQuery(this.TheListView); if ((viewFields != null) && (viewFields.Trim() != string.Empty)) { query.ViewFields = viewFields; } return this.TheList.GetItems(query); } catch (Exception exception) { this.AddError(exception); } return null; } ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------ 6. Getting A SharePoint Site Name ------------------------------------------------------------------------------------------------------------------------------ Description : This piece of code shows you how to retrieve a SharePoint Site name after using the OpenWeb() method. Code: public SiteData(string WebLocation) { this._theWeb = null; SPWeb web = null; try { web = new SPSite(theWebPath).OpenWeb(); string myString = web.Name; } catch (Exception exception) { Console.WriteLine("Error opening the site " + WebLocation + " : " + exception1.Message); } this._theWeb = web; } ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------