-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspfx-button.tsx
66 lines (59 loc) · 1.79 KB
/
spfx-button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as React from 'react';
import { useState } from 'react';
import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';
import { PrimaryButton } from '@fluentui/react/lib/Button';
import { MessageBar, MessageBarType } from '@fluentui/react/lib/MessageBar';
interface ICreateSiteButtonProps {
context: any;
}
const CreateSiteButton: React.FC<ICreateSiteButtonProps> = (props) => {
const [isCreating, setIsCreating] = useState(false);
const [message, setMessage] = useState('');
const createSite = async (): Promise<void> => {
setIsCreating(true);
setMessage('');
try {
// Call site creation endpoint
const response = await props.context.spHttpClient.post(
`${props.context.pageContext.web.absoluteUrl}/_api/SPSiteManager/create`,
SPHttpClient.configurations.v1,
{
body: JSON.stringify({
request: {
Title: "New Case Site",
Url: `sites/case-${Date.now()}`,
WebTemplate: "64",
SiteDesignId: "your-site-design-id"
}
})
}
);
if (response.ok) {
setMessage('Site created successfully!');
} else {
throw new Error(`Error: ${response.statusText}`);
}
} catch (error) {
setMessage(`Error creating site: ${error.message}`);
} finally {
setIsCreating(false);
}
};
return (
<div className="create-site-button">
<PrimaryButton
text="Create New Case Site"
onClick={createSite}
disabled={isCreating}
/>
{message && (
<MessageBar
messageBarType={message.includes('Error') ? MessageBarType.error : MessageBarType.success}
>
{message}
</MessageBar>
)}
</div>
);
};
export default CreateSiteButton;