Condividi tramite


Code Snippet: Add a Localized Name to a MetadataObject Using the Administration Object Model

Applies to: SharePoint Server 2010

In this article
Description
Prerequisites
To use this example

Description

The following code example shows how to programmatically add a localized name to a metadata object (model in this example) using the BDC Administration object model on the server.

Note

You can use the BDC Administration Client Object Model to create an external content type similarly on the client.

Prerequisites

  • Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 on the server.

  • Microsoft .NET Framework 3.5 and Microsoft Visual Studio on the client computer.

To use this example

  1. Start Visual Studio and create a C# Console application project. Select .NET Framework 3.5 when you create the project.

  2. From the View menu, click Property Pages to bring up the project properties.

  3. In the Build tab, for the Platform target, select Any CPU.

  4. Close the project properties window.

  5. In Solution Explorer, under References, remove all project references except for System and System.Core.

  6. Add the following references to the project:

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

  7. Replace the autogenerated code in Program.cs with the code listed at the end of this procedure.

  8. Replace the "<siteUrl>" string value with a valid SharePoint site name.

  9. Replace the "<EntityNamespace>" and "<EntityName>" string values with a namespace and entity name for an existing entity.

  10. Save the project.

  11. Compile and run the project.

    Note

    For a list of locales and locale IDs, see Locale IDs Assigned by Microsoft.

using System;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.SharePoint.BusinessData.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Microsoft.SDK.SharePoint.Samples.Bdc.AddLocalizedName
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the Catalog for the SharePoint site.
            BdcService service =
                SPFarm.Local.Services.GetValue<BdcService>(String.Empty);
            SPSite site = new SPSite("<siteUrl>");
            SPServiceContext context = SPServiceContext.GetContext(site);
            AdministrationMetadataCatalog catalog =
                service.GetAdministrationMetadataCatalog(context);

            // Retrieve an existing entity.
            Entity entity = 
                catalog.GetEntity("<entityNamespace>", "<entityName>");

            // Add a localized name to entity.
            Console.WriteLine("Localized name to add: ");
            string entityLozalizedName = Console.ReadLine();
            Console.WriteLine("Adding localized name to entity: "
                + entity.Name);

            // Add localized name for the current culture.
            entity.LocalizedDisplayName = entityLozalizedName;

            // Add localized name to specific culture.
            entity.LocalizedDisplayNames.Add(5555, entityLozalizedName);

            entity.Update();
            if (entity.ContainsLocalizedDisplayName())
                Console.WriteLine("Entity " + entity.Name +
                    " was updated with localized name " +
                    entity.LocalizedDisplayName);

        }
    }
}

See Also

Reference

BdcService

Services

AdministrationMetadataCatalog

GetAdministrationMetadataCatalog(SPServiceContext)

Entity

GetEntity(String, String)

LocalizedDisplayName

LocalizedDisplayNames

Update()

ContainsLocalizedDisplayName()