SPSecurityTrimmedControl

23. June 2010

SPSecurityTrimmedControl is a useful SharePoint control to control the visibility of content and controls on your site. In order to use this control, simply add the following code into your page:

 

<SharePoint:SPSecurityTrimmedControl runat="server" PermissionsString="ManageLists">
Your code here.
</SharePoint:SPSecurityTrimmedControl>

The list of the permissions is:

 

Member name Description
EmptyMask Has no permissions on the Web site. Not available through the user interface.
ViewListItems View items in lists, documents in document libraries, and view Web discussion comments.
AddListItems Add items to lists, add documents to document libraries, and add Web discussion comments.
EditListItems Edit items in lists, edit documents in document libraries, edit Web discussion comments in documents, and customize Web Part Pages in document libraries.
DeleteListItems Delete items from a list, documents from a document library, and Web discussion comments in documents.
ApproveItems Approve a minor version of a list item or document.
OpenItems View the source of documents with server-side file handlers.
ViewVersions View past versions of a list item or document.
DeleteVersions Delete past versions of a list item or document.
CancelCheckout Discard or check in a document which is checked out to another user.
ManagePersonalViews Create, change, and delete personal views of lists.
ManageLists Create and delete lists, add or remove columns in a list, and add or remove public views of a list.
ViewFormPages View forms, views, and application pages, and enumerate lists.
Open Allow users to open a Web site, list, or folder to access items inside that container.
ViewPages View pages in a Web site.
AddAndCustomizePages Add, change, or delete HTML pages or Web Part Pages, and edit the Web site using a SharePoint Foundation–compatible editor.
ApplyThemeAndBorder Apply a theme or borders to the entire Web site.
ApplyStyleSheets Apply a style sheet (.css file) to the Web site.
ViewUsageData View reports on Web site usage.
CreateSSCSite Create a Web site using Self-Service Site Creation.
ManageSubwebs Create subsites such as team sites, Meeting Workspace sites, and Document Workspace sites.
CreateGroups Create a group of users that can be used anywhere within the site collection.
ManagePermissions Create and change permission levels on the Web site and assign permissions to users and groups.
BrowseDirectories Enumerate files and folders in a Web site using Microsoft Office SharePoint Designer 2007 and WebDAV interfaces.
BrowseUserInfo View information about users of the Web site.
AddDelPrivateWebParts Add or remove personal Web Parts on a Web Part Page.
UpdatePersonalWebParts Update Web Parts to display personalized information.
ManageWeb Grant the ability to perform all administration tasks for the Web site as well as manage content. Activate, deactivate, or edit properties of Web site scoped Features through the object model or through the user interface (UI). When granted on the root Web site of a site collection, activate, deactivate, or edit properties of site collection scoped Features through the object model. To browse to the Site Collection Features page and activate or deactivate site collection scoped Features through the UI, you must be a site collection administrator.
UseClientIntegration Use features that launch client applications; otherwise, users must work on documents locally and upload changes.
UseRemoteAPIs Use SOAP, WebDAV, or Microsoft Office SharePoint Designer 2007 interfaces to access the Web site.
ManageAlerts Manage alerts for all users of the Web site.
CreateAlerts Create e-mail alerts.
EditMyUserInfo Allows a user to change his or her user information, such as adding a picture.
EnumeratePermissions Enumerate permissions on the Web site, list, folder, document, or list item.
FullMask Has all permissions on the Web site. Not available through the user interface.

Sharepoint

Log to SharePoint log file.

15. June 2010

1. Create a project (It seems .net 4 doesn't work well with SharePoint 2007)
2. Add the reference to Microsoft.Office.Server.dll. (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI)
3. The log code looks like this.

try
{
  throw new Exception("This is a test");
}
catch (Exception ex)
{
  Microsoft.Office.Server.Diagnostics.PortalLog.LogString(ex.Message, null);
}

Done

Sharepoint ,

Dave Wollerman's Post about SSO on SharePoint

10. June 2010

I find this Post from Dave Wollerman's blog. I think it is very useful to me. Here is the link SharePoint 2007 Single Sign-On Setup

Sharepoint

How to configure Forms Authentication in SharePoint 2007?

9. June 2010

Here is the step you need to do in order to config your SharePoint site using form authentication.

Preparation: You should have a existing ASP.net 2.0 membership database. Here is the link about how to create this database.
There is a existing user testAdmin already created in the membership database.
SQLConnectString in web.config:
<connectionStrings>
<add name="ApplicationServices"  connectionString="Data Source=localhost;User ID=sqluser;Password=Password1;persist security info=False;initial catalog=DemoUser;"  providerName="System.Data.SqlClient" />
</connectionStrings>

Membership Configuration Section in web.config:
<membership>
  <providers>
 <clear/>
 <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
  enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
  maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
  applicationName="/" />
  </providers>
</membership>

Step 1. Create a web application and a site collection.

1. Open the SharePoint Central Administration site.
2. Click Application Management tab.
3. Click Create or extend Web application
4. Click Create a new Web Application
5. In the Port textbox, put the port number you want. I am going to use Port 112 in this example.
6. Under "Allow anonymous", Click "Yes".
7. Click Ok.
8. After you get the information says the web application has been successfully create. Click the "Create Site Collection". I am going to create a publishing portal in this example. 
9. Enter the site title. For site template, select the "Publishing Portal" under the Publishing tab.
10. Click Ok.

Step 2. 
1. Open the site we just create. In this example it will be http://localhost:112. 
2. Click the "Enable anonymous access" link in my front page.

3. Click "Entire Web Site".

Step 3. Change the SharePoint authentication method
1. Open the SharePoint Central Administration Site.
2. Click the "Application Management" Tab.
3. Click the "Authentication Providers" under the "Application Security" section.

4. Confirm the web application you want to modify is correct. In this case http://localhost:112
5. Click the Default
6. Change the Authentication Type to "Forms"
7. Set the Membership Provider Name to "AspNetSqlMembershipProvider"
8. Set the Role Manager Name to "AspNetSqlRoleProvider"

9. Click Save.

Step 4 This is step is optional. You only need to do this if you want to set users from your membership database as your site collection administrators.
1. Open the web.config file for your Central Administration. The file is located in "C:\Inetpub\wwwroot\wss\VirtualDirectories\PortNumber" - PortNumber is the port number for your Central Administration site.
2. Add the SQLConnectString and Membership Configuration Section in the web.config.
3. Save and close your web.config file.
4. Open the SharePoint Central Administration Site.
5. Click the "Application Management" Tab.
6. Click "Site Collection Administrators" under the "SharePoint Site Management" Section.
7. Confirm the site collection you want to modify is correct. In this case http://localhost:112
8. For primary site collection administrator type "test" and verify user.  It may take a few seconds, but the page should underline the text in the textbox indicating that the username is valid.
9. Click Ok.

Now if you open the SharePoint site, you should able to see a "Sign In" link at the top of you screen.

Click that link you will able to see your login screen.

Then the screen should look like this. If you configure the step 4, you should be able to see the "Site Actions" menus as well.

Sharepoint ,

How to do bulk deletion in SharePoint?

20. April 2010

How to bulk delete in SharePoint?

The normal way to delete file programmatically in SharePoint is using the SharePoint API. The code looks like:

foreach(SPList myList in myWeb)
{
  foreach( SPListItem item in myList )
  {
    item.Delete();
  }
}

This works fine for document libraries contain small amount of data. It will be extremely slow when you have document libraries contain millions of records. In this case, you will need bulk deletion.

In order to use bulk deletion you need to know the CAML and how to use SPWeb.ProcessBatchData() methods.
There is the example.

private static String BuildBatchDeleteCommand( SPList list, SPListItemCollection coll )
{
  StringBuilder sbDelete = new StringBuilder();
  sbDelete.Append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>" );
 
  // We prepare a String.Format with a String.Format, this is why we have a {{0}}
  string command = String.Format( "<Method><SetList Scope=\"Request\">{0}</SetList><SetVar Name=\"ID\">{{0}}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{{1}}</SetVar></Method>", list.ID );
  foreach ( SPListItem item in coll )
  {
    sbDelete.Append( string.Format( command, item.ID.ToString(), item.File.ServerRelativeUrl ) );
  }
  sbDelete.Append( "</Batch>" );
  return sbDelete.ToString();
}

// While there's something left
while ( list.ItemCount > 0 )
{
  // We get everything but we limit the result to 100 rows
  SPQuery q = new SPQuery();
  q.RowLimit = 100;
 
  // We get the results
  SPListItemCollection coll = list.GetItems( q );
 
  // We process a CAML batch
  String batch = BuildBatchDeleteCommand( list, coll );
 
  // We execute it
  web.ProcessBatchData( batch );
 
  list.Update();
}

BTW the methods returns a String, if your code doesn’t work, it would return you an error code. If everything is fine, it just return "0x00000000".

Programming, Sharepoint

Basic about SharePoint Query

12. March 2010

When querying a list in SharePoint, the SPList.GetItems method is a simple and relatively easy way to return a list of records from the SharePoint List (returned as a SPListItemCollection). This is implemented simply as follows:

    using (SPWeb currentWeb = currentSite.OpenWeb(_RelativeWebUrl))
    {
           SPList currentList = currentWeb.Lists[_ListName]; 
           SPListItemCollection items = currentList.GetItems();
    }

As you progress a bit, you will find that you won't necessarily want to return all the items in a list. And the performance is not good when your list has a large amount of items. That's when using the SPQuery object comes in real handy. Simply write the CAML query for the code that you wish to return, then pass the SPQuery object to the GetItems method. This should then return the exact subset of data that you were after. (

    using (SPWeb currentWeb = currentSite.OpenWeb(_RelativeWebUrl))
    {
           SPList currentList = currentWeb.Lists[_ListName];
           SPQuery query = new SPQuery();                   
           query.Query = String.Format("<Where><Eq><FieldRef Name=\"{0}\" /><Value Type=\"{1}\">{2}</Value></Eq></Where>",_InternalName, _Type,_Value);    
           SPListItemCollection items = currentList.GetItems(query);
    }

HOWEVER, you must be careful that you write your CAML code correctly, or the method will return all records! This really surprised me and is a strange 'feature' one would think - I personally would have thought this should have returned NO records if it the query was incorrect. So basically if you find that every time you run a query there is a full record set returning, then check your CAML query...

U2U CAML Query Builder - It is a wonderful tool to test your CAML query.

Technology, Sharepoint