Thursday
May132010

Creating a custom list definition and instance for SharePoint 2010

1. Open your Visual Studio 2010 SharePoint project and add an Empty Element to your project. This is where you will define your fields for that will be in your custom list.

image

2. In the <Elements> tag add a field node for each column you want to add. For example to add SiteRelativeUrl Text field and Ordinal Number field your elements.xml would look like the following:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field ID="{5744d18c-305e-4632-8bd2-09d13ff4830d}"
    Type="Number"
    Name="Ordinal"
    DisplayName="Ordinal"
    Group="ConnectProject">
  </Field>
  <Field ID="{5744d18c-305e-4632-8bd3-09d134f4830e}"
    Type="Note"
    Name="ServerRelativeUrl"
    DisplayName="ServerRelativeUrl"
    Group="ConnectProject">
  </Field>
</Elements>

3. Add a new content type to your project with base type Item.

image

4. Modify the elements file in your content type by adding FieldRef nodes for the fields you created in the previous step.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x01006f40dd23b61d4cc5b84ee16d666c0943"
               Name="ConnectProject - Menu From List ContentType"
               Group="ConnectProject Content Types"
               Description="ConnectProject menu from list content type"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{5744d18c-305e-4632-8bd2-09d13ff4830d}"
               Name="Ordinal"
               DisplayName="Ordinal"
               Required="TRUE"/>
      <FieldRef ID="{5744d18c-305e-4632-8bd3-09d134f4830e}"
               Name="ServerRelativeUrl"
               DisplayName="ServerRelativeUrl"
               Required="TRUE"/>
    </FieldRefs>
  </ContentType>
</Elements>

5. Add a “list definition from content type” to the project

image image

6. Edit the list instance title and url and you should now have your custom list next time you deploy and activate your feature.

Friday
Apr162010

Visual Studio 2010: Code Generation

 

T4 (Text Template Transformation Toolkit) is a code generation framework that is included in Visual Studio. In this example I will show you how to create entity classes based off of a database without typing a line of code. This may save you weeks of development time on your next project.

Step 1. Create the database. In this case I am using a SQL Express Database.

image

Step 2. Add a new ADO.Net entity model to your project based off of the database.

image image

 

Step 3. Add a code generated item by right clicking in the entity model –> Add Code Generation Item –> ADO.Net EntityObject Generator. This will add a T4 template that will generate objects based on your entity model.

image 

Regeneration: The entity object code can be regenerated when changes are made by right clicking the EntityObject Generator tt file and selecting ‘Run Custom Tool’. The code will also be regenerated every time the tt file is saved.

image

Wednesday
Mar032010

Switching layouts when screen orientation changes in Android

 

When a displays orientation is changed you may find that your original layout doesn’t scale well. Every time the device orientation changes you current activity is destroyed and recreated. You can change your layout based on the current screen orientation in onCreate()

private void setContentBasedOnLayout()
{
    WindowManager winMan = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    
    if (winMan != null)
    {
        int orientation = winMan.getDefaultDisplay().getOrientation();
        
        if (orientation == 0) {
            // Portrait
            setContentView(R.layout.alertdialogportrait);
        }
        else if (orientation == 1) {
            // Landscape
            setContentView(R.layout.alertdialoglandscape);
        }            
    }
}

For more information see Wei-Meng Lee’s article on Dev-X: http://www.devx.com/wireless/Article/40792/0/page/1

Monday
Feb082010

Moving a SharePoint site collection to another managed path

To move a SharePoint site collection to another location you can run the following commands

 

1. stsadm –o backup –url <site collection url> –filename <filename to store backup>

2. stsadm –o deletesite –url <site collection url>

3. stsadm –o restore –url <site collection url> –filename <filename to restore from>

You must delete the original site before restoring to avoid errors.

Saturday
Feb062010

Battery Conscious: Using your GPS with Android

With that Android OS it is straight forward to use your GPS to get your location. Starting out you may get your location manager and register for updates so your program get’s notified of changes in position. It may look something like this:

LocationManager locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new LocationListener()
{
    public void onLocationChanged(Location location)
    {
        if (location != null)
        {
            Toast.makeText(getBaseContext(),
            "Position: latitude [" +
            location.getLatitude() +
            "] longitude [" + location.getLongitude()+"]",
            Toast.LENGTH_SHORT).show();
        }
    }
 
    public void onProviderDisabled(String provider)
    {
    }
 
    public void onProviderEnabled(String provider)
    {
    }
 
    public void onStatusChanged(String provider,
    int status, Bundle extras)
    {
    }
 
};
 
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locListener);


This is a typical example you might see floating around on the internet or in an Android Pro book. The line that requests location updates takes the following parameters

 

1. GPS Provider

2. Minimum time between updates in milliseconds

3. Minimum distance between updates in meters

4. Location Listener

 

The above implementation will actually keep you GPS active all the time and drain your battery in less than 5 hours. It also doesn’t take into account your actual needs when specifying the location provider, in this case the GPS. Let’s take a look at how to improve this code to save battery life.

The first thing you can do is set up criteria based on your needs to get the best GPS provider available. For instance, if you only need to get your location (lat, lon) without any information on bearing or speed, and require that the battery have at least a medium charge to use the GPS then you can do so. The following example uses Criteria to get the best GPS provider and limits the time and distance between updates to save battery life.

private void SetupGPSListener()
{
    Criteria criteria = new Criteria();
    
    criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    
    criteria.setAltitudeRequired(false);
    criteria.setSpeedRequired(false);
    criteria.setBearingRequired(false);
    
    // Indicates whether the provider is allowed to incur monetary cost.
    criteria.setCostAllowed(true);
    
    // Most location sensitive apps will need to be reactive to user movement.
    // Simply polling the Location Manager will not force it to get new updates from the Location Providers.
    // Use the requestLocationUpdates method to get updates whenever the current location changes
    
    // the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, 
    // and actual time between location updates may be greater or lesser than this value.
    int minTimeBetweenUpdatesms = 1000 * 60;
    
    // the minimum distance interval for notifications, in meters
    int minDistanceBetweenUpdatesMeters = 20; // 100 ft
    
    // Enable location providers
    // This method enables each provider and returns the last known location.
    // Also requests periodic updates for each provider to force android to start updating the locations for the other applications.
    String provider = locationManager.getBestProvider(criteria, true);
            
    locationManager.requestLocationUpdates(provider, minTimeBetweenUpdatesms, minDistanceBetweenUpdatesMeters, new LocationListener() {
        
        public void onLocationChanged(Location location) {
            currentLatitude = location.getLatitude();
            currentLongitude = location.getLongitude();                    
        }
 
        public void onProviderDisabled(String provider) {
            // Update application if provider disabled.
            Log.i(LOG_TAG, "GPS Provider is disabled");
        }
 
        public void onProviderEnabled(String provider) {
            // Update application if provider enabled.
            Log.i(LOG_TAG, "GPS Provider is enabled");
        }
 
        public void onStatusChanged(String provider, int status, Bundle extras) {
            
        }                
    });    
}