Find a table by name into SQL Server 2000 and SQLServer 2005

Monday, 20 July 2009 12:31 by myro

In SQL Server 2005 you can get a list of all your Tables with a particular name using the system table sys.table:

SELECT *

FROM sys.tables

WHERE name LIKE '%YOUR_TABLE_NAME%'

but if you need to do the same task in a SQL Server 2000, you should use

use YOURDB

select *

from INFORMATION_SCHEMA.TABLES

where TABLE_NAME LIKE '%YOUR_TABLE_NAME%'

because sys.table is not supported into SQL 2000.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  
Categories:   Sql
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed

How to delete Content Type in SharePoint 2007 when you receive "The Content type is in use" error

Thursday, 16 July 2009 17:19 by myro

A common problem in SharePoint 2007 is when you need to delete a Content Type from a List or from a Document Library: you receive the error 'The Content type is in use'. Ok, you understand that there is still a List or a Document Library that is using it and even if you remove all the associations from all your lists if you try to delete it you could receive again the same error.... 'The Content type is in use'
What's going on?
If you look at the msdn you will read that:

A content type cannot be deleted from a list if that list contains items of that content type.

So the problem is that you still have items in your List that are associated to that Content Type. A fast workaround is to select all the items, send them to the site's recycle bin, remove the Content Type and finally restore all those items from the bin back to the List.

msdn reference: http://msdn.microsoft.com/en-us/library/bb802962.aspx

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  
Categories:   SharePoint 2007
Actions:   Bookmark and Share | Permalink | Comments (2) | Comment RSSRSS comment feed

How to use SPFieldLookup to get the selected SPListItem reference in SharePoint 2007

Wednesday, 15 July 2009 17:13 by myro

A common task during SharePoint's developing is to read from a SharePoint List a item's (SPListItem) value defined in column. It's not a big deal if you information is stored into a text field:


SPList list = SPContext.Current.Web.Lists["List with lookup column"];

foreach (SPListItem item in list.Items)
{
    string information =  (string)item["YourColum"]
}

This is an easy and fast approch that works well if your column (SPField) is defined as text (SPFieldText), but what happens if you try to read from a different column type, such as from a LookUp Field? What you will get is a rappresentation of that value with this syntax:  ID;#TITLE .

Whould it be much better to obtain directly the referenced SPListItem, so you can do whatever you need? Learn how efficently read values from SPFields from this code snippet which is focused to read from lookup fields, but this kind of implementation can be applied on every SPField type:


SPList list = SPContext.Current.Web.Lists["List with lookup column"];

foreach (SPListItem item in list.Items)
{
  

    if (item.Fields["YourLookUpField"] is SPFieldLookup)
    {
        SPFieldLookup field = (SPFieldLookup)item.Fields[YourLookUpField];
        Guid webId = field.LookupWebId;
        Guid listId = new Guid(field.LookupList);
        int itemId = Int32.Parse(item[YourLookUpField].ToString().Split(';')[0]);
        SPListItem lookupItem = null;
        // here is you reference...
        lookupItem = SPContext.Current.Site.OpenWeb(webId).Lists[listId].GetItemById(itemId);
        // do something...for this sample i will get the item's  title
        string title = lookupItem.Title;
    }
}

So remember that when you need to read values from SpListItems, first check your SPField's type,  the cast it and then obtain the value you need.

Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   SharePoint 2007
Actions:   Bookmark and Share | Permalink | Comments (2) | Comment RSSRSS comment feed

Using PickerTreeDialog in SharePoint 2007 pages with postback support

Tuesday, 14 July 2009 11:07 by myro

In this post you will learn how to implement a Picker Tree Dialog into your custom SharePoint 2007's pages and handle the postbacks generated when and user press the OK or the Cancel button. Searching on the web i've found differents implementations, but none of them cares about postbacks that are generated once you submit the form.
If you didn't recognize what is a SharePoint's Picker Tree Dialog, look at this image:

This control comes only if yopu are using SharePoint's Moss (standard and enterprise) versions and is not avaible in Windows SharePoint Services (WSS). It allows you to browse the entire SiteCollection and to select Sites, Lists and Document Libraries. Now you will learn how to retreive the selected items on server side using postbacks.

I will show how to use this control into a SharePoint's custom Page that is located under the /_layouts/ directory, but it won't be complicated to implement this tecnique into a SharePoint's WebPart. Let's start....

First you need to import a directive for SharePoint's dll into the ASPX markup page:

<%@ Import namespace="Microsoft.SharePoint" %>

and then add a reference to the PickerTreeDialog.js:

<script type="text/javascript" language="javascript"
      
 src='<%= String.Format("/_layouts/{0}/PickerTreeDialog.js", SPContext.Current.Web.Locale.LCID.ToString()) %>'>
</
script>

As you see you can see, I use a localized PickerTreeDialog, so it doesn't matter if your web site is in english or in a different culture.
Next you need to add 2 javascript funcions that will care on showing up the Picker Tree Dialog and handling the Postbacks caused from pressing the OK button.
Add this 2 JavaScript functions:

<script type="text/javascript">

    function performPostBack(param) {
        if (param.toString().indexOf("DELETE") == 0) {
            if (confirm('Are you sure you want to delete this configuration?')) {
                __doPostBack("<%= this.UniqueID %>", param);
            }
        }
        else {
            __doPostBack("<%= this.UniqueID %>", param);
        }
    }
   
  
    function launchPicker()
    {
       var url = "<%= SPContext.Current.Web.Url %>";
       var callback = function(arr)
       {
            if (arr == null || arr == undefined) {
            return;
            }
           var strparam = "PICKER,";
           for (indx in arr) {
               strparam = strparam + arr[indx] + "§";
           }
            performPostBack(strparam);
        }

        LaunchPickerTreeDialog('CbqPickerSelectListTitle',
        'CbqPickerSelectListTitle', '', "",
         url, null, "", "", "/_layouts/images/generic.png", 0, callback);
    }

</script>  

The performPostBack() funcion will perform the Page's postback and launchPicker() function will pop up the control.
To invoke the launchPicker() function, let's use a classic Asp.Net Button control:

<asp:Button runat="server" CausesValidation="false" Text="..." OnClientClick="javascript:launchPicker();" />


Now we need to implement the IPostBackEventHandler interface into the page's code Behind, to be able to capture postback's paramters from server side.

Use the interface:

 public partial class YourPageName : System.Web.UI.Page, IPostBackEventHandler

and implement the required method:

 public void RaisePostBackEvent(string eventArgument)
{
    string[] events = eventArgument.Split(',');

    switch (events[0].Trim())
    {
     
       case "PICKER":
       PickerParameterParser(events[1]);
       break;
    }

}

private void PickerParameterParser(string pickerParams)
{
   string[] param = pickerParams.Split(new char[] { '§' }, StringSplitOptions.RemoveEmptyEntries);        
}

Now, everytime the Ok button is pressed your PickerParameterParser() method will be invoked and the param[]  array will contain all the PickerTreeDialog parameters that are passed using the callback.

Lets's see what happens everytime you select a different item from the Picker Tree Dialog:

The Root site has been selected:

param[0] = Area:?Type:Guid:
param[1] = SiteRelativeUrl

example:

param[0] = Area:?SPWeb:33cc66fc-c251-47a1-984a-3bb7e751a6cb:
param[1] = /

A sub site has been selected:

param[0] = Type:Guid:
param[1] = SiteRelativeUrl

example:

param[0] = SPWeb:fd00f025-b7df-4eae-83c0-7aec11c9929c:
param[1] = /YourSubSiteName

A List has been selected:

param[0] = Type:Guid:ParentType:ParentGuid
param[1] = SiteRelativeUrl
param[2] = ListName

example:

param[0] = SPList:a525db03-551b-44c4-9450-e7add15e510c?SPWeb:33cc66fc-c251-47a1-984a-3bb7e751a6cb:
param[1] = /
param[2] = YourListName

It's not hard to understand that a possibile PickerParameterParser implementation could be something like:

private void PickerParameterParser (string pickerParams)
{
    string[] param = pickerParams.Split(new char[] { '§' }, StringSplitOptions.RemoveEmptyEntries);
    if (param[0].StartsWith("Area"))
    {
        // add your code to handle when a rootsite is selected
    }
    else if (param[0].StartsWith("SPWeb"))
    {
        // add your code to handle when a SharePoint site is selected
    }
    else if (param[0].StartsWith("SPList"))
    {
        // add your code to handle when a SHarePoint List or Document library
        // is selected
        // Here's an example: let's write into a TextBox, the selected list's url:
        //texBoxListUrl.Text = String.Concat(SPContext.Current.Site.Url,
        //                SPContext.Current.Site.OpenWeb(param[1]).Lists[param[2]].DefaultViewUrl); 
    }
   
}

Have fun...

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   SharePoint 2007
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed

Add or Remove WebParts into SharePoint 2007 on runtime using SPLimitedWebPartManager

Wednesday, 8 July 2009 16:30 by myro

If you want to install or remove SharePoint's WebParts on runtime using SharePoint's object model, you need first of all get a reference to the page's SPLimitedWebPartManager. This reference can be easy obtained through the SPWeb object, invoking the GetLimitedWebPartManager() method. Here's a code snippet:

string AbsolutePageUrl = "http://YourSite/Page.aspx";
using (SPSite site = new SPSite(AbsolutePageUrl))
{
   using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
   {
     SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
     System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
   }
}

Once you got your SPLimitedWebPartManager you can add or remove your WebParts by calling the AddWebPart or DeleteWebPart methods.
Imagine that you have installed in the farm a custom WebPart and now you need to insert it or delete it from a SharePoint's page. Let's says that your WebPart is named MyWebPart and  declared as:

 public class MyWebPart : Microsoft.SharePoint.WebPartPages.WebPart

The code snippets that you need to use for accomplishing this task are:

Adding a WebPart

string AbsolutePageUrl = "http://YourSite/Page.aspx";
using (SPSite site = new SPSite(AbsolutePageUrl))
{
   using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
   {
     SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
     System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);


     // if MyWebPart is already installed, leave!
     foreach (var webpart in SpWebPartManger.WebParts)
     {
          if (webpart is MyWebPart)
              return;
     }
     // else add it in the bottom!
     SpWebPartManger.AddWebPart(new MyWebPart(), "Main",10);
     SpWebPartManger.Dispose();

   }
}

Removing a WebPart

string AbsolutePageUrl = "http://YourSite/Page.aspx";
using (SPSite site = new SPSite(AbsolutePageUrl))
{
   using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
   {
     SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
     System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

      WebPart webPartToDelete = null;
      // let's locate the webpart first....
      foreach (WebPart webpart in SpWebPartMaanger.WebParts)
      {
            if (webpart is MyWebPart)
            {
                webPartToDelete = webpart;
                break;
            }
       }

       if (webPartToDelete != null)
             SpWebPartManger.DeleteWebPart(webPartToDelete);
       SpWebPartManger.Dispose();

   }
}

Have fun...

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   SharePoint 2007
Actions:   Bookmark and Share | Permalink | Comments (2) | Comment RSSRSS comment feed