Compartilhar via


Code Snippet: Implementing a Creator

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

Example for a .NET Connectivity Assembly

public String CreateCustomer(Customer customer)
{
    customers.Add(customer);
    return customer.CustomerID;
}

Example for an ASP.NET Web Service

[WebMethod]
public String CreateCustomer(Customer customer)
{
    customers.Add(customer);
    return customer.CustomerID;
}

Example for a WCF Service

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

[OperationContract]
string CreateCustomer(Customer customer);

The following example shows the implementation of the method instance.

public String CreateCustomer(Customer customer)
{
    customers.Add(customer);
    return customer.CustomerID;
}

Additional Code Examples

External System—.NET Connectivity Assembly

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

public static Contact Create(Contact newContact)
{
    const string ServerName = "MySQLServerName";
    AdventureWorksDataContext dataContext = new AdventureWorksDataContext
          ("Data Source=" + ServerName + ";" +
           "Initial Catalog=AdventureWorks;Integrated Security=True");

    Contact contact = new Contact();

    contact.FirstName = newContact.FirstName;
    contact.LastName = newContact.LastName;
    contact.EmailAddress = newContact.EmailAddress;
    contact.Phone = newContact.Phone;
    contact.EmailPromotion = newContact.EmailPromotion;
    contact.NameStyle = newContact.NameStyle;
    contact.PasswordHash = newContact.PasswordHash;
    contact.PasswordSalt = newContact.PasswordSalt;
    contact.ModifiedDate = DateTime.Now;
    contact.rowguid = Guid.NewGuid();

    dataContext.Contacts.InsertOnSubmit(contact);
    dataContext.SubmitChanges();
    return contact;
}

See Also

Concepts

Implementing a Creator