Code Snippet: Implementing a SpecificFinder

Applies to: SharePoint Server 2010

In this article
Example for a .NET Connectivity Assembly
Example for an ASP.NET Web Service
Example for a WCF Service
Additional Code Examples

The following code examples show how you can implement a SpecificFinder method instance in a .NET connectivity assembly and in a Web service.

Example for a .NET Connectivity Assembly

public Customer GetCustomerByID(String id)
{
    foreach (Customer customer in customers)
    {
        if (customer.CustomerID.Equals(id) && !customer.IsDeleted)
        {
            return customer;
        }
    }

    throw new ArgumentOutOfRangeException("id", "Customer not found");
}   

Example for an ASP.NET Web Service

[WebMethod]
public Customer GetCustomerByID(String id)
{
    foreach (Customer customer in customers)
    {
        if (customer.CustomerID.Equals(id) && !customer.IsDeleted)
        {
            return customer;
        }
    }

    throw new ArgumentOutOfRangeException("id", "Customer not found");
}

Example for a WCF Service

The following code shows the operation definition in the service contract interface.

[OperationContract]
Customer GetCustomerByID(string id);

The following example shows the implementation of the method instance.

public Customer GetCustomerByID(String id)
{
    foreach (Customer customer in customers)
    {
        if (customer.CustomerID.Equals(id) && !customer.IsDeleted)
        {
            return customer;
        }
    }

    throw new ArgumentOutOfRangeException("id", "Customer not found");
}

Additional Code Examples

  1. External System—Database/SQL Server

    For example, for the Contact entity in a Microsoft SQL Server database, the SpecificFinder method might look similar to the following.

    public static Contact ReadItem(int contactID)
    {
        const string ServerName = "MySQLServerName";
        AdventureWorksDataContext dataContext = new AdventureWorksDataContext
              ("Data Source=" + ServerName + ";" +
               "Initial Catalog=AdventureWorks;Integrated Security=True");
    
        Contact Contact =
            (from contacts in dataContext.Contacts.AsEnumerable()
             where contacts.ContactID == contactID
             select contacts).Single();
        return Contact;
    }
    
  2. External System—Flat File

    public static IEnumerable<FlatFileEntity> ReadList()
    {
        List<FlatFileEntity> flatFileEntityList = new List<FlatFileEntity>();
        TextReader textReader = 
            new StreamReader(@"c:\data\flat-file-data-source.txt");
        string row;
    
        while ((row = textReader.ReadLine()) != null)
        {
            FlatFileEntity flatFileEntity = new FlatFileEntity();
    
            string[] entityData = row.Split(',');
    
            flatFileEntity.ID = entityData[0];
            flatFileEntity.Company = entityData[1];
            flatFileEntity.FirstName = entityData[2];
            flatFileEntity.LastName = entityData[3];
            flatFileEntity.Address = entityData[4];
            flatFileEntity.City = entityData[5];
            flatFileEntity.State = entityData[6];
            flatFileEntity.ZipCode = entityData[7];
            flatFileEntity.Phone = entityData[8];
            flatFileEntity.LastUpdated = DateTime.Parse(entityData[9]);
            flatFileEntityList.Add(flatFileEntity);
        }
    
        textReader.Close();
    
        foreach (FlatFileEntity entity in flatFileEntityList)
        {
            if (entity.ID == id)
                return entity;
        }
        return null;
    }
    

See Also

Concepts

Implementing a SpecificFinder