다음을 통해 공유


Code Snippet: Implementing a Finder

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 Finder method instance in a .NET connectivity assembly and in a Web service.

Example for a .NET Connectivity Assembly

public Customer[] GetCustomers(String name, int? limit)
{
    if (name != "")
    {
        List<Customer> customersWithName = new List<Customer>();

        foreach (Customer customer in customers)
        {
            //$Name$
            if (name.StartsWith("$"))
            {
                if (name.EndsWith("$"))
                {
                    if (customer.Name.Contains(name.Substring(1, 
                        (name.Length - 2))))
                    {
                        customersWithName.Add(customer);
                    }
                }
                else
                {
                    if (customer.Name.EndsWith(name.Substring(1, 
                        (name.Length - 1))))
                    {
                        customersWithName.Add(customer);
                    }
                }
            }
            else if (name.EndsWith("$"))
            {
                if (customer.Name.StartsWith(name.Substring(0, 
                    (name.Length - 1))))
                {
                    customersWithName.Add(customer);
                }
            }
            //Name
            else if (customer.Name == name)
            {
                customersWithName.Add(customer);
            }
        }

        if (limit < 1)
            return customersWithName.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customersWithName.Count;
                i++)
                limitedcustomers.Add(customersWithName[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();

        }
    }

    else
    {
        if (limit < 1)
            return customers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customers.Count; i++)
                limitedcustomers.Add(customers[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        }
    }
}

Example for an ASP.NET Web Service

[WebMethod]
public Customer[] GetCustomers(String name, int? limit)
{
    if (name != "")
    {
        List<Customer> customersWithName = new List<Customer>();

        foreach (Customer customer in customers)
        {
            //$Name$
            if (name.StartsWith("$"))
            {
                if (name.EndsWith("$"))
                {
                    if (customer.Name.Contains(
                        name.Substring(1, (name.Length - 2))))
                    {
                        customersWithName.Add(customer);
                    }
                }
                else
                {
                    if (customer.Name.EndsWith(
                        name.Substring(1, (name.Length - 1))))
                    {
                        customersWithName.Add(customer);
                    }
                }
            }
            else if (name.EndsWith("$"))
            {
                if (customer.Name.StartsWith(
                    name.Substring(0, (name.Length - 1))))
                {
                    customersWithName.Add(customer);
                }
            }
            //Name
            else if (customer.Name == name)
            {
                customersWithName.Add(customer);
            }
        }

        if (limit < 1)
            return customersWithName.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customersWithName.Count; 
                i++)
                limitedcustomers.Add(customersWithName[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();

        }
    }

    else
    {
        if (limit <1)
            return customers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customers.Count; 
                i++)
                limitedcustomers.Add(customers[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        }
    }
}

Example for a WCF Service

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

[OperationContract]
Customer[] GetCustomers(string name, int? limit);

The following example shows the implementation of the method instance.

public Customer[] GetCustomers(String name, int? limit)
{
    if (name != "")
    {
        List<Customer> customersWithName = new List<Customer>();

        foreach (Customer customer in customers)
        {
            //$Name$
            if (name.StartsWith("$"))
            {
                if (name.EndsWith("$"))
                {
                    if (customer.Name.Contains(
                        name.Substring(1, (name.Length - 2))))
                    {
                        customersWithName.Add(customer);
                    }
                }
                else
                {
                    if (customer.Name.EndsWith(
                        name.Substring(1, (name.Length - 1))))
                    {
                        customersWithName.Add(customer);
                    }
                }
            }
            else if (name.EndsWith("$"))
            {
                if (customer.Name.StartsWith(
                    name.Substring(0, (name.Length - 1))))
                {
                    customersWithName.Add(customer);
                }
            }
            //Name
            else if (customer.Name == name)
            {
                customersWithName.Add(customer);
            }
        }

        if (limit < 1)
            return customersWithName.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customersWithName.Count; 
                i++)
                limitedcustomers.Add(customersWithName[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();

        }
    }

    else
    {
        if (limit < 1)
            return customers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customers.Count; i++)
                limitedcustomers.Add(customers[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        }
    }
}

Additional Code Examples

  1. External System—Database/SQL Server

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

    public static IEnumerable<Contact> ReadList(int itemLimit)
    {
        const string ServerName = "MySQLServerName";
        AdventureWorksDataContext dataContext = new AdventureWorksDataContext
              ("Data Source=" + ServerName + ";" +
               "Initial Catalog=AdventureWorks;Integrated Security=True");
    
        IEnumerable<Contact> Contacts =
            from contacts in dataContext.Contacts.Take(itemLimit)
            select contacts;
        return Contacts;
    
    }
    

    The following is a slightly more complex example of a Finder method that has filterable parameters.

    SELECT ProductID, Name, ProductNumber, ListPrice FROM Product 
    WHERE (ProductID >= @MinProductID) AND (ProductID <= @MaxProductID) 
    AND (Name LIKE @Name) AND ProductNumber 
    LIKE @ProductNumber)
    
  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();
    
        return flatFileEntityList.toArray();
    }
    
  3. Paging Sample

    public static IEnumerable<Document> ReadFolder(
        string parentFolderID, int pageNumber)
    {
        //Required to allow SQL CE to work in an ASP.NET environment
        AppDomain.CurrentDomain.SetData(
            "SQLServerCompactEditionUnderWebHosting", true);
    
        DMSEntities ctx = new DMSEntities(connString);
    
        int tid = int.Parse(parentFolderID);
        string username = GetCurrentDMSUsername();
    
        /* SQL CE does not support paging so we must return all results,
           create an anonymous type, and then take the page. In production,
           you would do this all in a single LINQ statement. */
        var a = ((from d in ctx.Documents
                  where (d.ParentId == tid)
                  select new { 
                      d.Name, d.Id, d.ParentId, d.IsFolder }).
                      OrderBy(d => !d.IsFolder)).ToList();
    
        //Get authorized documents by page.
        var p = a.Where(c => 
            IsAuthorized(username, c.Id) == true).
            Skip((pageNumber - 1) * PageSize).Take(PageSize);
    
        List<Document> docs = new List<Document>();
    
        foreach (var i in p)
        {
            Document doc = new Document();
            doc.ID = i.Id.ToString();
            doc.Name = i.Name;
            doc.ParentID = i.ParentId.ToString();
            if (i.IsFolder)
                doc.Type = "Folder";
            else
                doc.Type = "Document";
            doc.Version = "Published";
    
            docs.Add(doc);
        }
    
        return docs;
    }
    

See Also

Concepts

Implementing a Finder