Sharepoint Insight by Namwar Rizvi

Sharepoint Tips, Tricks and inside knowledge from real world experience by Namwar Rizvi

Archive for January, 2009

How to get all available search contexts for the current site collection?

Posted by namwar on January 26, 2009


If you want to retrieve all available search contexts for the web site collection where your application is initiated then you can use the following code to get it

SearchContext searchContext = SearchContext.GetContext(ServerContext.Current);

Posted in Uncategorized | Leave a Comment »

How to hide user name in Sharepoint Survey?

Posted by namwar on January 20, 2009


If you want to hide the identity of the user who responded your survey, just perform the following steps:

  1. Navigate to your survey page
  2. Click Settings and then click Survey Settings
  3. survey-settings

  4. Under General Settings click Title, description and navigation link
  5. general-settings

  6. Under Survey Options, select No for the question Show user names in survey results?
  7. show-user-names

  8. Click Save

Now, check your survey and you will see that all user identity fields like Created By and Last Modified By will just show *** instead of the original user name.

Posted in Management | Tagged: , , | Leave a Comment »

How Sharepoint ranks a document in Search Results?

Posted by namwar on January 12, 2009


Several times I have been asked about the way Sharepoint calculates the rank of a document in document search. Although, it is based on a complex probablistic algorithm developed by Microsft Researcher, following are some simple tips to know the background of search ranking and how to tune the search results. Please note, these tips are not an authentic statements from Microsoft, instead they are my observations and knowledge gained from other fellow Sharepoint experts.

According to Microsoft on Technet in this article

Ranking depends on:

  • The length of the document
  • The frequency of the query term in the entire collection of documents
  • The number of documents containing the query term
  • The number of documents in the entire collection of documents
     

Keeping the above points in mind, following observations have been made as mentioned in this blog

  • Higher size of the document = lower ranking
  • More frequent the keyword matched in a document = higher ranking
  • Deeper URL surf depth = lower ranking
  • Authoritative pages in SP = higher ranking
  • Default views seem to get ranked higher than individual item views
  • File types seem to affect ranking (.doc > .txt for instance)
  • Language seems to affect ranking, US-EN is always higher, even though your server installation may have french as it’s default language

Posted in Architecture, SSP | Tagged: , , , | Leave a Comment »

How to solve “Unable to connect publishing custom string handler for output caching”

Posted by namwar on January 11, 2009


If you are getting “Unable to connect publishing custom string handler for output caching” error in your event log then it is because of a custom web service you have installed on your Sharepoint server. Reason for this error is that you have a web part on your portal page which use the custom web service. Now, since Sharepoint uses output caching for the web pages to optimize the rendering experience and fast delivery of unchanged pages therefore, publishing infrastructure of Sharepoint tries to access the web service through your web part and due to any issue in your service, it starts generating this error.

Anyway, cutting a long story short, here is the solution:

Edit the web.config of custom web service (Please do not edit your Sharepoint Web application web.config) and make sure you have following under httpModules node which is located under system.web node. 

            <remove name=PublishingHttpModule />



Posted in Errors, Management | Tagged: , , , , | 7 Comments »

Programmatically upload a file to Document Library

Posted by namwar on January 10, 2009


Following is a utility function which you can use to upload a file programmatically in SharePoint document library. It has two parameters. First is the source file path and second is the target document library path. 

Following is an example call to this function:

UploadFileToDocumentLibrary(@”C:\test.txt”, @”http://home-vs/Shared Documents/textfile.txt”);

and here is the function

    public static bool UploadFileToDocumentLibrary(string sourceFilePath, string targetDocumentLibraryPath)

    {

        //Flag to indicate whether file was uploaded successfuly or not

        bool isUploaded = true;

        try

        {

            // Create a PUT Web request to upload the file.

            WebRequest request = WebRequest.Create(targetDocumentLibraryPath);

 

            //Set credentials of the current security context

            request.Credentials = CredentialCache.DefaultCredentials;

            request.Method = “PUT”;

 

            // Create buffer to transfer file

            byte[] fileBuffer = new byte[1024];

 

            // Write the contents of the local file to the request stream.

            using (Stream stream = request.GetRequestStream())

            {

                //Load the content from local file to stream

                using (FileStream fsWorkbook = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read))

                {

                    //Get the start point

                    int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length);

                    for (int i = startBuffer; i > 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length))

                    {

                        stream.Write(fileBuffer, 0, i);

                    }

 

                }

            }

 

            // Perform the PUT request

            WebResponse response = request.GetResponse();

 

            //Close response

            response.Close();

        }

        catch (Exception ex)

        {

            //Set the flag to indiacte failure in uploading

            isUploaded = false;

        }

 

        //Return the final upload status

        return isUploaded;

    }

Posted in C# | Tagged: , | 12 Comments »

How to debug custom application pages in Sharepoint

Posted by namwar on January 9, 2009


To debug your custom application pages located in layouts folder, perform the following steps:

  1. In IIS, select your web application and from context menu, select Open. It will display the directory in windows explorer
  2. Edit Web.config in any text editor (e.g. Notepad)
  3. In web.config, search for compilation node like “<compilation batch=”
  4. To enable debugging, update the node as follows
    • Set batch=false
    • Set debug=true
  5. After update, your compilation node will become <!– /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:””; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:”Times New Roman”; mso-fareast-font-family:”Times New Roman”;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:35.4pt; mso-footer-margin:35.4pt; mso-paper-source:0;} div.Section1 {page:Section1;} –>

    <compilation batch=false debug=false>

  6. Launch Internet Explorer and browse your custom application page
  7. Now, launch Visual Studio and open your Custom Application Page from layouts directory
  8. Go to Debug->Attach To Process and attach to w3wp.exe
  9. attachtoprocess1
    attachtoprocess2

  10. Add break points as necessary.
  11. In Internet Explorer, refresh your page and bingo! you can debug your custom application page now.

Posted in Debugging | 5 Comments »

SQL Server 2005 Service Pack 3 and Sharepoint 2007

Posted by namwar on January 1, 2009


If you are using Sharepoint 2007 to manage and display your reports then there is a big chance you may be using Reporting Services in Sharepoint Integrated mode. You may have noticed that reports run slower in Sharepoint integrated mode as compared to native mode. This is because of the calls to Sharepoint object model in integrated mode. To improve the performace Microsoft has released some optimizations in Service Pack 3 of SQL Server 2005 which reduces the number of calls to object model and increases the overall response  time and performace.

Happy New Year to all readers and best wishes for 2009!

Posted in Uncategorized | Tagged: , , | Leave a Comment »