Wednesday, January 22, 2014

People Search using Keyword Query in SharePoint 2013

Its been a long time since I have blogged. I was busy with some personal activities and now I have free time to start blogging!!
Here is the scenario.  I am developing an application which connects to user profile service a lot and I need to do a lots of filtering/searching based on the custom properties.
One solution could be to use user profile service and then do the work. You will find a lot of sample available online to do it.
One approach (which is efficient) is to use the result sources which has been created for us in SharePoint. Here is the code which I have written to get all the user profiles:
=============
private void button1_Click(object sender, EventArgs e)
        {
            SPSite sp = new SPSite("http://Test:8077/sites/esc/");
// For all employees Use DataTable dt = GetPeople(sp, "*”);         
   DataTable dt = GetPeople(sp, "EmployeeID:" + "101");
            dataGridView1.DataSource = dt;
        }
        private DataTable GetPeople(SPSite spSite, string queryText)
        {
            var keywordQuery = new KeywordQuery(spSite)
            {
                QueryText = queryText,
                KeywordInclusion = KeywordInclusion.AllKeywords,
                SourceId = new Guid("B09A7990-05EA-4AF9-81EF-EDFAB16C4E31")
            };
              keywordQuery.RowLimit = 300;
            keywordQuery.SelectProperties.Add("AccountName");
            keywordQuery.SelectProperties.Add("BloodGroup");
            keywordQuery.SelectProperties.Add("Designation");
            keywordQuery.SelectProperties.Add("SubjectOfExpertise");
            keywordQuery.SelectProperties.Add("PhoneNumber");
            keywordQuery.SelectProperties.Add("MarriageAnniversary");
            keywordQuery.SelectProperties.Add("BoardLineNumber");
            keywordQuery.SelectProperties.Add("EmployeeID");
            SearchExecutor e = new SearchExecutor();
            ResultTableCollection rt = e.ExecuteQuery(keywordQuery);
            var tab = rt.Filter("TableType", KnownTableTypes.RelevantResults);
            var result = tab.FirstOrDefault();
            DataTable DT = result.Table;
            return DT;
        }
========================
The B09A7990-05EA-4AF9-81EF-EDFAB16C4E31 is for People Search. Complete list can be found at:  http://sharepoint.ulitzer.com/node/2501170
If you want to search other sources, just change the guid.
Use the above code and let me know if this is helpful.

Friday, January 17, 2014

SHAREPOINT 2013 KEYWORD QUERY (KQL) CONTENT CLASS PROPERTY RESTRICTIONS

Using property restrictions in your KQL queries, you can restrict your search to only pull back certain things like calendar events for instance. This is a very powerful way to limit search results and get exactly what you are looking for.

You can use these in your query like this:
“lunch contentclass:STS_ListItem_Events“
This will return only calendar events with the word “lunch” in them.  Pretty powerful and pretty simple.

Here is a list of available content class items:

STS_Site –  Site Collection
STS_Web  –  Site (Web)
STS_List_850  –  Page Library
STS_ListItem_850  –  Page
STS_List_DocumentLibrary  –  Document Library
STS_ListItem_DocumentLibrary  –  Document Library Items
STS_List  –  Custom List
STS_ListItem  –  Custom List Item
STS_List_Links  –  Links List
STS_ListItem_Links  –  Links List Item
STS_List_Tasks  –  Tasks List
STS_ListItem_Tasks  –  Tasks List Item
STS_List_Events  –  Events List
STS_ListItem_Events  –  Events List Item
STS_List_Announcements  –  Announcements List
STS_ListItem_Announcements  –  Announcements List Item
STS_List_Contacts  –  Contacts List
STS_ListItem_Contacts  –  Contacts List Item
STS_List_DiscussionBoard  –  Discussion List
STS_ListItem_DiscussionBoard  –  Discussion List Item
STS_List_IssueTracking  –  Issue Tracking List
STS_ListItem_IssueTracking  –  Issue Tracking List Item
STS_List_GanttTasks  –  Project Tasks List
STS_ListItem_GanttTasks  –  Project Tasks List Item
STS_List_Survey  –  Survey List
STS_ListItem_Survey  –  Survey List Item
STS_List_PictureLibrary  –  Picture Library
STS_ListItem_PictureLibrary  –  Picture Library Item
STS_List_WebPageLibrary  –  Web Page Library
STS_ListItem_WebPageLibrary  –  Web Page Library Item
STS_List_XMLForm  –  Form Library
STS_ListItem_XMLForm  –  Form Library Item
urn:content-class:SPSSearchQuery  –  Search Query
urn:content-class:SPSListing:News  –  News Listing
urn:content-class:SPSPeople  –  People
urn:content-classes:SPSCategory  –  Category
urn:content-classes:SPSListing  –  Listing
urn:content-classes:SPSPersonListing  –  Person Listing
urn:content-classes:SPSTextListing  –  Text Listing
urn:content-classes:SPSSiteListing  –  Site Listing
urn:content-classes:SPSSiteRegistry  –  Site Registry Listing

Wednesday, July 31, 2013

SharePoint 2010 - Hide Custom Div in Model Popup Window

Hi folks,

In model Popup Window, if we have any unwanted div from master page need to hide. we have to follow the bellow class which i mentioned in red color.

class="s4-notdlg"
>....


Thursday, March 14, 2013

How to find SharePoint List and Libraries true Size?




Overview
I wanted to get size of list and libraries for my portal in a reporting fashion. For Publishing Portal site collection you can access the list and library size from site actions> under the Site Collection Administration group, you have access to “Storage space allocation”. This is great info. But not something I can consume the same information programmatically.
image
If you notice this page is server through /_layouts/storman.aspx. Further digging on this page gives me the inherited  name space “Microsoft.SharePoint.ApplicationPages.StorMan” which is obfuscated and hence not sure how this above data is retrieved.
 

Further exploration

So still hoping to find the little nugget that is producing this information, I started diggings in to the Content Database Stored Procedures.
Then I came across two interesting stored procedures: proc_GetListSizes and proc_GetDocLibrarySizes
Both stored procedures take the Site Collection GUID. When test ran, the result was the same as the above Storage space allocation page.
 

So what I did with these stored procedures?

I took these two stored procedures, ran against the content database of my portal site collection. Merged the results of both stored procedures. Used some rudimentary caching  to store and retain the results set. Now you can list or get size for your given SPList object in MB.

Warning

This is not recommended against the production system as this is not a Microsoft Supported operation. Also be aware that the results could be huge and may impact your system.

Well here is all you are waiting for… the code base:

I have tested this against the MOSS 2007 and have not had chance to validate for any other SharePoint versions yet.
  • I have also included the Web Lists Size GetWebSizeWithUnitMeasure().
  • I have also included the Lists Size GetListSizeInBytes().
  • Then I have also included a function to determined the best way to represent the size in Bytes or KB or MB or GB or TB DefineSizeWithUnitMeasure

Your calls examples

To get size of a list:
string SizeWithUnitMeasure;
double webSizeInBytes = GetWebSizeWithUnitMeasure(web, out withUnitMeasure);
//Use the SizeWithUnitMeasure to print with the measure.


To get size of a web:


string listSizeWithMeasure;
double listSizeInBytes = GetListSizeWithUnit(list, out listSizeWithMeasure);
//Use the listSizeWithMeasureto print with the measure.



Common Function




 




public static double DefineSizeWithUnitMeasure(double sizeInBytes, out string unitMeasure)
       {
           unitMeasure = "Bytes";
           double size = sizeInBytes;

           if (size > 1024)
           {
               size = sizeInBytes / 1024d;//KB
               unitMeasure = "KB";
           }
           if (size > 1024)
           {
               size = size / 1024d;//MB
               unitMeasure = "MB";
           }
           if (size > 1024)
           {
               size = size / 1024d; //GB
               unitMeasure = "GB";
           }

           if (size > 1024)
           {
               size = size / 1024d; //TB
               unitMeasure = "TB";
           }

           return size;
       }

       public static double GetWebSizeWithUnitMeasure(SPWeb web, out string withUnitMeasure)
       {
       
           double storageUsage = 0d;

           
           foreach (SPList list in web.Lists)
           {
               storageUsage += (double) GetListSizeInBytes(list);
           }
           


           string unitMeasure = "";
           double webSize = DefineSizeWithUnitMeasure(storageUsage, out unitMeasure);

           withUnitMeasure = string.Format("{0} {1}", webSize.ToString("f"), unitMeasure);

           return storageUsage;
       }
       

       


       public static double GetListSizeWithUnit(SPList list, out string withUnitMeasure )
       {
           double listSizeinBytes = (double) GetListSizeInBytes(list);
           string unitMeasure = "";
           double listSize = DefineSizeWithUnitMeasure(listSizeinBytes, out unitMeasure);

           withUnitMeasure=string.Format("{0} {1}", listSize.ToString("f"), unitMeasure);

           return listSizeinBytes;
       }



        public static long GetListSizeInBytes(SPList list)
        {
            long listSize = 0;

            string filter = string.Format("tp_id='{0}'", list.ID);

            DataTable myDataTable = GetCachedSiteCollectionListSizes(list.ParentWeb.Site);
            DataRow[] dataRows = myDataTable.Select(filter);

            if (dataRows.Length > 0)
            {
                listSize = (long)dataRows[0]["TotalSize"];
            }

            return listSize;
        }



        private static DataTable m_SiteCollectionListSizes;
        private static Guid m_SiteCollectionListSizesSiteID;

        private static DataTable GetCachedSiteCollectionListSizes(SPSite site)
        {
            if (m_SiteCollectionListSizes == null || m_SiteCollectionListSizesSiteID != site.ID)
            {
                m_SiteCollectionListSizes = GetSiteCollectionListSizes(site);
                m_SiteCollectionListSizesSiteID = site.ID;
            }

            return m_SiteCollectionListSizes;

        }

        private static DataTable GetSiteCollectionListSizes(SPSite site)
        {

            DataTable dataTable = GetDocLibSizes(site);
            //Combine both list and doc lib size results
            dataTable.Merge(GetListSizes(site));
            
            return dataTable;

        }

        private static DataTable GetDocLibSizes(SPSite site)
        {
           
            string connectionString = site.WebApplication.ContentDatabases[site.ContentDatabase.Id].DatabaseConnectionString;


           string storedProcName = "proc_GetDocLibrarySizes";
            
            System.Data.SqlClient.SqlConnection connection = null;
            System.Data.SqlClient.SqlDataReader reader = null;
            DataTable dataTable = null;
            
            try
            {
                connection = new System.Data.SqlClient.SqlConnection(connectionString);
                connection.Open();

                System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(storedProcName, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SiteId", site.ID.ToString()));

                reader = command.ExecuteReader();

                dataTable = new DataTable();
                dataTable.Load(reader);

            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (connection != null)
                    connection.Close();
            }
            return dataTable;
        }

        private static DataTable GetListSizes(SPSite site)
        {

            string connectionString = site.WebApplication.ContentDatabases[site.ContentDatabase.Id].DatabaseConnectionString;
            string storedProcName = "proc_GetListSizes";

            System.Data.SqlClient.SqlConnection connection = null;
            System.Data.SqlClient.SqlDataReader reader = null;
            DataTable dataTable = null;

            try
            {
                connection = new System.Data.SqlClient.SqlConnection(connectionString);
                connection.Open();

                System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(storedProcName, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SiteId", site.ID.ToString()));

                reader = command.ExecuteReader();

                dataTable = new DataTable();
                dataTable.Load(reader);
               
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (connection != null)
                    connection.Close();
            }
            return dataTable;
        }

Friday, March 8, 2013

Search Configuration Best Practices in SharePoint 2010


This is the best practice I had find to configure search in SharePoint 2010:
    1. Add a crawl component to a Search Service Application
1)    In Central Administration, in the Application Management section, clickManage service applications.
2)    On the Service Applications page, click the name of the Search Service Application to which you want to add a crawl component.
3)    On the Search Administration page, in the Search Application Topologysection, click the Modify button.
noteNote: The SharePoint Search topology cannot be changed in Standalone installations.
4)     On the Manage Search Topology page, click New, and then click Crawl Component.
5)    In the Add Crawl Component dialog box, in the Server list, click the farm server to which you want to add the crawl component.
6)    In the Associated Crawl Database list, click the crawl database you want to associate with the new crawl component.
7)    In the Temporary Location of Index field, you can optionally enter the location on the server that will be used for creating the index files before propagating them to the query components. If you want to accept the default location, leave the contents of this field unchanged.
8)    Click OK to add the new crawl component to the job queue.
9)    On the Manage Search Topology page, click the Apply Topology Changesbutton to start the SharePoint timer job that will add the new crawl component to the farm on the specified server.
    1. Create or Edit a content source.  
From Search Administration Page, in the Crawling section at the quick navigation bar, click Content Sources.

To create a content source

1.    On the Manage Content Sources page, click New Content Source.
2.    On the Add Content Source page, in the Name section, in the Name box, type a name for the new content source.
3.    In the Content Source Type section, select the type of content that you want to crawl.
4.    In the Start Addresses section, in the Type start addresses below (one per line) box, type the URLs from which the crawler should begin crawling.
5.    In the Crawl Settings section, select the crawling behavior that you want.
6.    In the Crawl Schedules section, to specify a schedule for full crawls, select a defined schedule from the Full Crawl list. A full crawl crawls all content that is specified by the content source, regardless of whether the content has changed. To define a full crawl schedule, click Create schedule.
7.    To specify a schedule for incremental crawls, select a defined schedule from the Incremental Crawl list. An incremental crawl crawls content that is specified by the content source that has changed since the last crawl. To define a schedule, click Create schedule. You can change a defined schedule by clicking Edit schedule.
8.    To prioritize this content source, in the Content Source Priority section, on the Priority list, select Normal or High.
9.    To immediately begin a full crawl, in the Start Full Crawl section, select the Start full crawl of this content source check box, and then click OK.

 

To edit a content source

1.    You can edit a content source to change the schedule on which the content is crawled, the crawl start addresses, the content source priority, or the name of the crawl. Crawl settings and content type cannot be changed when editing a content source.
2.    On the Manage Content Sources page, in the list of content sources, point to the name of the content source that you want to edit, click the arrow that appears, and then click Edit.
3.    After you have made the changes that you want, select the Start full crawl of this content source check box, and then click OK.
    1. Add some crawling rules to include or exclude paths from crawling. 
Add rules to exclude the following paths:
    • http://*/_catalogs/*
    • http://*/_layouts/*
    • http://*/Lists/*
    • http://*/Documents/*
    • http://*/Forms/*
    • http://.*?/DocLib[0-9]*/.*?
Note: just for the last rule you have to check the option: “Use regular expression syntax for matching this rule”
You can add a rule, to exclude some URL, as in the following screenshot:
  Adding a rule to exclude some URL


Add the following rule to forcibly include pages as normal http pages. Select the option “Include all items in this path” And check the two options: “Crawl complex URLs” and “Crawl SharePoint content as normal http pages”:
    • http://*/pages/*.*
(Note: if you was using non publishing site you would then use “http://*/sitepages/*.*” and also the previous deny rules may have to be modified.)
You can do the previous include rule as in this screen shoot:
 Adding a rule to include all files that have extensions
           

Also, add the following rule to give the crawler the chance to browse the directories without including those directories in the search. Note: this role work with integration with the previous rule that include all files in the search.
You can do the previous include rule as in this screen shoot:
 Adding rule to allow searching inside directories without including the directories them self.


Unfortunately, the previous configuration will work well only for sites that do not have a redirection page at the root. An example, of a site that has a redirection page at the root, is a multilingual site with a variation redirection page at the root. However, if this is your case, simply do two things. First, replace the rule: "http://*/Forms/*" with an exclude rules for those three: "http://*/Forms/Thumbnails.aspx", "http://*/Forms/AllItems.aspx" and "http://*/Forms/DispForm.aspx". Second, do not use any include rules. This will make you rules list as following:
  • http://*/_catalogs/*
  • http://*/_layouts/*
  • http://*/Lists/*
  • http://*/Documents/*
  • http://*/Forms/Thumbnails.aspx
  • http://*/Forms/AllItems.aspx
  • http://*/Forms/DispForm.aspx
  • http://.*?/DocLib[0-9]*/.*?

 Note: just for the last rule you have to check the option: “Use regular expression syntax for matching this rule”.
Enjoy!

Wednesday, December 7, 2011

SharePoint 2007 Issues and Resolutions

SharePoint 2007 Issues and ResolutionsIssue:Accessing Company Sharepoint Site from internal LAN using IP address or Domain name Scenario:SharePoint intranet site called “mynews” is setup on port 8098 in one of the company’s internal server, and assigned a domain name, “moss.intranews.com”. The company’s off-shore team when try to access the site by URL (http://10.20.130.140:8090/) from their workstation, browser shows “page not found” or HTTP 404 error”. Solution:1. Access SharePoint 2007 Central Administration web page.2. Click on Operations tab3. Click on “Alternate access mappings” from Global Configuration section4. Click on Add Internal URLs5. Enter http://10.20.130.140:8090/6. Set to “Intranet”7. Restart the IIS Now try to access the site with IP address and should work. Alternate access mappings provide a mechanism for SharePoint administrators to identify the different ways in which user’s access portal sites, ensuring that URLs (links) are displayed appropriately for the manner in which the user accesses the portal site. Export and Import a SharePoint SubsiteHad a situation recently where I needed to take a backup of a Wiki subsite and restore it on another SharePoint site collection's subsite residing on a different farm. In WSS 3.0, there are two new STSADM commands - import and export.The basic procedure to restore the subsite was to STSADM export the site to a file and then STSADM import it to new site collection's sub site. Note: The toplevel site is http://sspserver1:4500/ and the subsite is http://sspserver1:4500/Wiki/ Export subsitestsadm -o export -url http://sspserver1:4500/Wiki/ -filename d:\DatabaseBackup\Wiki.bak -includeusersecurity -versions 4 Import subsitestsadm -o import -url http://sspserver2:4500/Wiki/ -filename d:\DatabaseBackup\Wiki.bak -includeusersecurity A couple of things to note:The -includeusersecurity switch ensures that all the columns such as modifed by, created by are maintained.The -versions 4 switch will ensure that all versions of list items/documents are exported. You must import/export to sites that share the same template. So a team site can only be exported and imported into another team site. You will know you are trying to import into non matching templates if you get the following error: "The exported site is based on the template STS#1 but the destination site is based on the template STS#0" This means that a site based on the blank template is trying to be imported into a site based on the team template.To my knowledge there is no easy way of changing a sites templates. The following lists some of the common WSS templates:Team Site STS#0 Blank Site STS#1 Document Workspace STS#2 Wiki Site WIKI#0 Blog BLOG#0 Error Message while Importing a sub site. A list, survey, discussion board, or document library cannot have the same name as another list, survey, discussion board, or document library in this Web site. To avoid the above error message while using STSADM import, create a subsite by choosing Blank Site template and then run the above stsadm import command. A list, survey, discussion board, or document library cannot have the same name as another list, survey, discussion board, or document library in this Web site. To avoid the above error message while using STSADM import, create a subsite by choosing Blank Site template and then run the above stsadm import command. Please use an unused port for this site error in Central Admin SiteMy Central Administration site in MOSS environment was giving me following error message after restarting the server.IIS was unable to start this site. Another site may already be using the port you configured for this site. Please use an unused port for this site. Looks like changing the port number through IIS won’t do any good.You will have to do this through the command line. First navigate to following location C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN You need to use STSADM with setadminport parameterType stsadm.exe -o setadminport –port [port number] Server Farm Configuration Not Complete I was getting above error message in bright red text on left hand top corner in my Central Administration page. I made sure following services are all running,Document Conversions Launcher Service Document Conversions Load Balancer Service Office SharePoint Server Search Windows SharePoint Services Help Search Windows SharePoint Services Incoming E-Mail Windows SharePoint Services Web ApplicationThen I check on the Check Services Enabled in this Farm page (Central Administration > Application Management > Check Services Enabled in this Farm) to identify any errors. Its fine too.Then I had to finally create the Shared Services. This make the error message disappear. Could not load XSL file. The system cannot find the file specified In attempting to setup the content query web part on a MySite, after exporting the web part and re-importing it to the MySite site I then got a "Could not load XSL file. The system cannot find the file specified" error. Go to site settings of MySite and enable the "Office SharePoint Server Publishing Infrastructure" site collection features. Now the content query web part would start working.Invalid or loopback address error I was trying to install MOSS on a SQL server environment and received above error message. The SQL Server database was installed on non default TCP port. So I need to type something like “Database Server name, TCP port” when I try to create the configuration databases. But it was failing with above error.I had to update database TCP port to default 1433 port to get SharePoint successfully installed.Changing Passwords for Office SharePoint Server Accounts with STSADMServer Farm AccountTo update the server farm account, run the following stsadm command on the server that hosts the Central Administration Web site.stsadm –o updatefarmcredentials –userlogin -password You must then run the iisreset /noforce command to restart the application pool.If you encounter a Service Unavailable error when you try to access the Central Administration Web site, you must also run the following stsadm command on every other server in your server farm.stsadm –o updatefarmcredentials –userlogin -password -localOther Application Pool AccountsTo update the application pool account, run the following stsadm command on the server that hosts the Central Administration Web site.stsadm –o updateaccountpassword –userlogin -password -noadminYou must then run the iisreset /noforce command to restart the application pool.Search Service AccountsYou can use the following stsadm command to update the password for the Office SharePoint Server Search service.stsadm –o osearch –farmserviceaccount -farmservicepassword Alternatively, you can reconfigure these accounts by using the Shared Services Administration section of the Central Administration Web site.Shared Services Provider AccountsIf you change the password for an SSP account, you must update the account details by using either the Central Administration Web site or the stsadm command-line tool. You can use the following stsadm command to update an SSP account.stsadm –o editssp –title -ssplogin -ssppassword You must then run the iisreset /noforce command to restart the SSP service.The list is displayed in Standard view. It cannot be displayed in Datasheet view for one or more of the following reasons: A datasheet component compatible with Windows SharePoint Services is not installed, your browser does not support ActiveX controls, or support for ActiveX controls is disabled. Central Administration > Application Management > Authentication Providers > Edit AuthenticationEnable Client Integration? - Choose "Yes" Above steps would resolve following 2 issues:1. Users will see from any List "Edit in Datasheet"2. Users will be able to see from Site Actions -> Create -> Custom List -> Import Spreadsheet To export a list, you must have a Windows SharePoint Services-compatible application.From SharePoint List, when I try to export a list to Excel, above error message is displayed.Internet Explorer is trying to use one of the OWSSUPP.DLL, so unregister one of them and try to export the list to Excel.c:\Program Files\Microsoft Office\OFFICE11\OWSSUPP.DLLc:\Program Files\Microsoft Office 2007\OFFICE12\OWSSUPP.DLLCannot connect to the server at this time. Your list cannot be publishedThis error is caused because some properties are not set properly in IIS.Right click on website SharePoint is running onSelect HTTP Headers TabEnsure you have the following properties in the Custom HTTP headersName : MicrosoftSharePointTeamServicesValue : 6.0.2.5530Restart IISShared Services Admin Site Access Denied Error I am taking over as the SharePoint Administrator, meaning I am working with another person creation. Everything has been okay, expect when I try to go to Shared Services. I have no idea what account was used to create this SSP because every account I try to log in with I keep getting an Access Denied error. Any ideas on how to gain access to this site?If you have administrative rights on the server, you can give yourself permission to get into the Shared Services.Central AdministrationApplication ManagementSite Collection Administrators,change to the site collection for the Shared Services administration (you will have to change the web application as well), and make yourself the primary or secondary site collection administrator.