简体   繁体   中英

How to set a user's default view from JavaScript in Microsoft Dynamics

I'd like to set a users default view for a table depending on their business unit.

I found out you can set the default view like this:

async function setDefaultView(viewId: string) {
  if(!viewId.startsWith('{') || !viewId.endsWith('}')) {
    throw new Error('Your viewId should probably start and end with curly braces like this: {00000000-0000-0000-0000-000000000000}');
  }

  const userId = Xrm.Utility.getGlobalContext().userSettings.userId.slice(1, -1); // Remove the curly braces from the user id, because ODATA doesn't want them
  await fetch(
    `${window.location.origin}/api/data/v9.0/usersettingscollection(${userId})`,
    {
      method: "PATCH",
      headers: { "Content-Type": "application/json; charset=utf-8" },
      body: JSON.stringify({
        personalizationsettings: `
          <DefaultGridViews>
            <DefaultGridView>
              <EntityTypeCode>1024</EntityTypeCode>
              <ChildEntityName></ChildEntityName>
              <QueueId></QueueId>
              <ViewId>${viewId}</ViewId>
              <ViewType>1039</ViewType>
            </DefaultGridView>
          </DefaultGridViews>
        `.replaceAll(/\s/g, '')
      })
    }
  );
}

Note : This shouldn't overwrite the personalization settings for other tables. When using patch, dynamics is smart enough to merge the personalizationsettings for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM