Skip to content

Commit fe72421

Browse files
Merge pull request #31 from microsoft/users/zhaodongwang/associateSolution
add solution association in create table
2 parents 31ec26f + 00e2228 commit fe72421

3 files changed

Lines changed: 45 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Auth:
4141
| `delete` | `delete(logical_name, id)` | `None` | Delete one record. |
4242
| `delete` | `delete(logical_name, list[id])` | `None` | Delete many (sequential). |
4343
| `query_sql` | `query_sql(sql)` | `list[dict]` | Constrained read-only SELECT via `?sql=`. |
44-
| `create_table` | `create_table(tablename, schema)` | `dict` | Creates custom table + columns. Friendly name (e.g. `SampleItem`) becomes schema `new_SampleItem`; explicit schema name (contains `_`) used as-is. |
44+
| `create_table` | `create_table(tablename, schema, solution_unique_name=None)` | `dict` | Creates custom table + columns. Friendly name (e.g. `SampleItem`) becomes schema `new_SampleItem`; explicit schema name (contains `_`) used as-is. Pass `solution_unique_name` to attach the table to a specific solution instead of the default solution. |
4545
| `create_column` | `create_column(tablename, columns)` | `list[str]` | Adds columns using a `{name: type}` mapping (same shape as `create_table` schema). Returns schema names for the created columns. |
4646
| `get_table_info` | `get_table_info(schema_name)` | `dict | None` | Basic table metadata by schema name (e.g. `new_SampleItem`). Friendly names not auto-converted. |
4747
| `list_tables` | `list_tables()` | `list[dict]` | Lists non-private tables. |
@@ -310,6 +310,7 @@ info = client.create_table(
310310
"active": "bool",
311311
"status": Status,
312312
},
313+
solution_unique_name="my_solution_unique_name", # optional: associate table with this solution
313314
)
314315

315316
# Create or delete columns

src/dataverse_sdk/client.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,12 @@ def get_table_info(self, tablename: str) -> Optional[Dict[str, Any]]:
373373
"""
374374
return self._get_odata()._get_table_info(tablename)
375375

376-
def create_table(self, tablename: str, schema: Dict[str, Any]) -> Dict[str, Any]:
376+
def create_table(
377+
self,
378+
tablename: str,
379+
schema: Dict[str, Any],
380+
solution_unique_name: Optional[str] = None,
381+
) -> Dict[str, Any]:
377382
"""
378383
Create a simple custom table with specified columns.
379384
@@ -397,6 +402,9 @@ class ItemStatus(IntEnum):
397402
}
398403
399404
:type schema: dict[str, Any]
405+
:param solution_unique_name: Optional solution unique name that should own the new table.
406+
When omitted the table is created in the default solution.
407+
:type solution_unique_name: str or None
400408
401409
:return: Dictionary containing table metadata including ``entity_schema``,
402410
``entity_set_name``, ``entity_logical_name``, ``metadata_id``, and ``columns_created``.
@@ -425,7 +433,11 @@ class ItemStatus(IntEnum):
425433
print(f"Created table: {result['entity_logical_name']}")
426434
print(f"Columns: {result['columns_created']}")
427435
"""
428-
return self._get_odata()._create_table(tablename, schema)
436+
return self._get_odata()._create_table(
437+
tablename,
438+
schema,
439+
solution_unique_name,
440+
)
429441

430442
def delete_table(self, tablename: str) -> None:
431443
"""

src/dataverse_sdk/odata.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,13 @@ def _get_entity_by_schema(self, schema_name: str) -> Optional[Dict[str, Any]]:
632632
items = r.json().get("value", [])
633633
return items[0] if items else None
634634

635-
def _create_entity(self, schema_name: str, display_name: str, attributes: List[Dict[str, Any]]) -> str:
635+
def _create_entity(
636+
self,
637+
schema_name: str,
638+
display_name: str,
639+
attributes: List[Dict[str, Any]],
640+
solution_unique_name: Optional[str] = None,
641+
) -> str:
636642
url = f"{self.api}/EntityDefinitions"
637643
payload = {
638644
"@odata.type": "Microsoft.Dynamics.CRM.EntityMetadata",
@@ -646,7 +652,10 @@ def _create_entity(self, schema_name: str, display_name: str, attributes: List[D
646652
"IsActivity": False,
647653
"Attributes": attributes,
648654
}
649-
r = self._request("post", url, json=payload)
655+
params = None
656+
if solution_unique_name:
657+
params = {"SolutionUniqueName": solution_unique_name}
658+
self._request("post", url, json=payload, params=params)
650659
ent = self._wait_for_entity_ready(schema_name)
651660
if not ent or not ent.get("EntitySetName"):
652661
raise RuntimeError(
@@ -1086,7 +1095,12 @@ def _delete_table(self, tablename: str) -> None:
10861095
url = f"{self.api}/EntityDefinitions({metadata_id})"
10871096
r = self._request("delete", url)
10881097

1089-
def _create_table(self, tablename: str, schema: Dict[str, Any]) -> Dict[str, Any]:
1098+
def _create_table(
1099+
self,
1100+
tablename: str,
1101+
schema: Dict[str, Any],
1102+
solution_unique_name: Optional[str] = None,
1103+
) -> Dict[str, Any]:
10901104
# Accept a friendly name and construct a default schema under 'new_'.
10911105
# If a full SchemaName is passed (contains '_'), use as-is.
10921106
entity_schema = self._normalize_entity_schema(tablename)
@@ -1110,7 +1124,18 @@ def _create_table(self, tablename: str, schema: Dict[str, Any]) -> Dict[str, Any
11101124
attributes.append(payload)
11111125
created_cols.append(attr_schema)
11121126

1113-
metadata_id = self._create_entity(entity_schema, tablename, attributes)
1127+
if solution_unique_name is not None:
1128+
if not isinstance(solution_unique_name, str):
1129+
raise TypeError("solution_unique_name must be a string when provided")
1130+
if not solution_unique_name:
1131+
raise ValueError("solution_unique_name cannot be empty")
1132+
1133+
metadata_id = self._create_entity(
1134+
entity_schema,
1135+
tablename,
1136+
attributes,
1137+
solution_unique_name,
1138+
)
11141139
ent2: Dict[str, Any] = self._wait_for_entity_ready(entity_schema) or {}
11151140
logical_name = ent2.get("LogicalName")
11161141

0 commit comments

Comments
 (0)