Showing posts with label MOSS. Show all posts
Showing posts with label MOSS. Show all posts

Friday, December 10, 2010

Working with SPQuery

As a sharepoint developer most of the people might have used SPQuery and CAML atleast once.

Writing a Basic Query

SPContext ctx = SPContext.Current;
using (SPSite site = new SPSite(ctx.Site.ID))
{
      using (SPWeb web = site.OpenWeb(webid))
      {
         SPList list = web.Lists["MyTestList];
         SPQuery query = new SPQuery();
         //You have to call GetItems function of SPlist to execute the CAML query
         SPListItemCollection items = list.GetItems(query);      
      }
}

Here it will return All the Items.But in real time we need to specify the conditions and attributes to fine tune the Results. We need to have basic understanding of CAML syntax inorder to take full advantage of SPQuery Object.If you have a CAMLBuilder tool then it will be always handy in writing CAML queries.You could douwnload CAML builder from here

Understanding SPQuery Properties

1. SPQuery.RowLimit
   This Property allows you to set how many rows you need in your result.Its quiet common that your Query could return more than 500 rows so its better to limit the number of rows in a result and use paging to retrive the rest of the result.I will explain paging below.

2. spquery.ViewAttributes

View attribute is one of the important attribute which is veryusefull to set how you are going to retrieve the Data.
  Scenario 1: You want retrieve all the items including items in folder and subfolders then set it like the following

 //If you want to return only file
          SPQuery.ViewAttributes = "Scope='Recursive'";   //If you want to return All items
          SPQuery.ViewAttributes = "Scope='RecursiveAll'";

  Scenerio 2 : You want retrive only files
          spquery.ViewAttributes = "Scope=\"FilesOnly\"";

  Scenario 3: Only Approved Items
         spquery.ViewAttributes = "ModerationType='HideUnapproved'"

3. SPQuery.Folder
   This property is usefull when you want to retrieve the data only from specific folder.In real world scenario you need to retrive the items from specific folder rather than retrieving all items in the list.
    SPQuery.Folder = folder; //It should be a SPFolder Object

 4.SPQuery.ViewFields
This property helps to set which are the fields /columns you want in the result.There is no point in returning all the coloumns.So its better practice to specify the fields you want to return.

     //Name is the coloumn name / internal name of the field
     SPQuery.ViewFields = "";

5. SPQuery.Query

This property used to set the real CAML query.Build your CAML string and assign to this property.

SPQuery.Query = String.Format("<Where>
                                                           <Eq>
                                                              <FieldRef Name=\'FileLeafRef\'/>
                                                              <Value Type=\"Text\"></Value>
                                                        </Eq>
                                                  </Where>", searchText);

//searchText is name of the file you want to search

Note : One thing here we have to remember is no need to add query tag in your XML.

If you are using Date time in your query then set includetimevalue = true. See Below

Query = "<Where><Eq><FieldRef Name='Modified'/><Value Type='DateTime' IncludeTimeValue='TRUE'>"+ LastCheckedDate + "</Value></Eq>
</Where>"

CAML Notations


•Eq = equal to
•Neq = not equal to
•BeginsWith = begins with
•Contains = contains
•Lt = less than
•Leq = less than or equal to
•Gt = greater than
•Geq = greater than or equal to
•IsNull = is null
•IsNotNull = is not null


Paging In SPQuery

Using paging with SPQuery is good practice.Its better to write code that will perform efficient way than giving bad user experience.

      using (SPSite site = new SPSite(SiteCollectionURL))
         {
                 
           using (SPWeb web = site.OpenWeb(WebName))
             {

              
               SPQuery query = new SPQuery();
               query.ViewAttributes = "Scope=\"Recursive\"";
               query.Folder = list.RootFolder;


               do
               {

                   SPListItemCollection listItems = list.GetItems(query);
                  
                 
//Perform the operations you want
                   query.ListItemCollectionPosition = listItems.ListItemCollectionPosition;
                 
//If you are showing in a grid view then stor the listItems.ListItemCollectionPosition in
                  viewstate or something like that
               } while (query.ListItemCollectionPosition != null);
             }
         }


Limitation of SPQuery Object
You cannot use the SPquery object to query across multiple site collection instead use SPSiteDataQuery

Monday, December 6, 2010

Installing MOSS 2007

MOSS Installation is straight forwar but at times, it could take you to a position where you need reinstall everything.Here i just trying to explain the option available at each step so you could install it with ease.

1. Double click on the MOSS Installer.

2. Click on RUN
3.Enter the product key and you could see tick mark changed to Green (If you entered right product key)
4. Click on Continue
5.Accept kBthe license agreement by checking the CheckBox


6. Click on Continue


7. In this screen You have two option BASIC and ADVANCED

Basic - If you are installing in a developer box or a system you could use for DEMO purpose choose the BASIC installation.It will install the SQL Server Express Edition and Sharepoint into the BOX.It will not allow you to configure much , more equivalent to a one click installation.

ADVANCED - Choose advanced option whenever you are installing in standared environment (DIT,SIT OR Production).This option allow you to configure Sharepoint installation properly.

8. Click on ADVANCED


9. Above screen prescent you with important option tabs in your installation
Server Type Tab -  prescent you with three options

1.Complete :- If  you are installing for any of the standared environment choose complete option.So it will install all  the component into the system.There only two scenario i could think if you are not choosing this option  you are adding a additional server to your Sharepoint Farm or you are setting up a development environment.

2. Web Front End - This option you will choose only when adding web front end server to your sharepointfarm , means you already have complete installation and sharepoint central admin site is running in that server and want to aad additional server for scaling you farm.If you setting up the first BOX(Server) in a your farm then you should go with Complete option.

3. Stand alone - If you are setting up a developer box or demo box you coul choose this option.Because you dont have any plan to scale this by adding another server.Dont use this option for any of your standared environments.

File Location Tab prescent you with the option

 In File location Tab you could specify where all the physical files of sharepoint installation goes to.Its equivalent any installation package which allow you to specify the physical location of the files.Normaly sharepoint installation will take around 8GB so if you prefer to keep the installation other than c: drive you could specify that and you could also isolate the sharepoint installation to a specific drive so it will easy for the maintainence.

Feed Back :- Good to keep this enabled so microsoft could improve from feedback.

10. Choos Complete Option
11. Click on Install Now
12. You will the screen with a progress Bar. Once installation Complete it will present with following screen


13.If you want to run the Sharepoint configuration Wizard then keep the check box checked.If you want to run it later (After a cup of tea) uncheck the option.

 

Now we installed the sharepoint but we need to configure the sharepoint .So sharepoint could store all the configuration information in its configuration Database and run based on the configuration.

You could strat the configuration wizard at any point of time by going to Administrative Tools-> Sharepoint Poduct and Technologies Configuration Wizard.



1. Click On Next
2.Click on Yes


3.This step you could executing in different scenario

Executing Config Wizard First Time
In this case it will ask you the DataBase Sever Name , Config DB name and User Name and Password to connect. Provide those information according to your environment

Executing Config Wizard Again (Re running the config)
 Inthis case it will prescent you with above screen with DataBase Sever Name , Config DB name prepopulated and greyed.  you could select do not disconnect and run the wizard.If you want to create every thing from sratch choose disconnect.

Executing Config Wizard To disconnect from a farm.
  Inthis case it will prescent you with above screen  with DataBase Sever Name , Config DB name prepopulated and greyed. So you could select  Disconnect and run the wizard.

Executing Config Wizard After Service Pack or Patch Installation
Inthis case it will prescent you with above screen with DataBase Sever Name , Config DB name prepopulated and greyed. you could select do not disconnect and run the wizard.

4. Click on Next



5. Above screen allow you to set the port number in which you are going to run the Central admin site.Give a port number which you could remember easily.

6.Above screen also allows you to choose what kind of authentication you are going to use for Sharepoint.If you didnt have any specific need to use Kerberose go with NTLM.If you are going to use Kerberose then you are going to make the configuration more complex.Here i am not going to cover the Kerberos because it requires seperate article for that.

7. Click On Next



8. Verify the information provided are correct and click Next

9.You will see 8 stage  configuration Going on something like below


10.After success full completion you will see the following screen


Now the time to Go to you central admin Site and Do the rest of Configuration.

Sunday, August 23, 2009

Sharepoint PropertyBag

Sharepoint comes with handfull of option to store the information with ease.When we are thinking about storing configuration information then first item comes to our mind is web.config.But when we look at sharepoint perspective, there is one more usfull option called propertybag is available.It can store the information at different levels such as


• Farm
• Server
• Web Application
• Site Collection
• Site
• List
So we can share the information across different web where a list failed to do that and also able to persist information across farm.

SPWeb exposes two properties to store the information one is AllProperties which equivalent to a hashtable and supports case sensitive keys.Second one is properties (PropertyBag) and no supports for case sensitive keys.
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web= site.OpenWeb()) {
// Add a property entry
web.Properties[key] = value;
web.AllProperties[key] = value;
web.Update();
web.Properties.Update();

// Remove a property entry
web.AllProperties.Remove(key);
web.Properties[key] = null;
web.Update();
web.Properties.Update();
}
}
We can also serialize the object and store in the propertybags.Sounds cool !!
You can download Property bag settings from http://pbs.codeplex.com/ and more details about managing configuration information http://spg.codeplex.com/Wiki/View.aspx?title=Managing%20Application%20Configuration&referringTitle=Home