Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a problem creating Sites on Sharepoint Online. The code that works on-premise do not work on Sharepoint Online.

I do not want to bother you with the code, i just want a clean start.

So my question is:

How do you create a site on Sharepoint Online programmatically?

--------------SOLUTION---------------

The reason why it works sometimes:

The inconsistenty your are expericing is because, if the first time your script run it takes 20 sec to run. If you in that time start it again, it would have to wait on the first to finish. But it still only has 30 secounds to execute including the time spent waiting. So the first script toke 20 sec, than there is only 10 secounds left for a 20 sec job, and it will then get terminated. That is the reason why it works sometimes.

--------------EDIT--------------

Here is the code - It works on on-premise:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EventReceiverProject1.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);

           DateTime dt = DateTime.Now;
           String strDate = "";
           strDate = dt.ToString("HH.mm.ss");

           properties.AfterProperties["Title"] = "Kevi2n Changed by event receiver at" + strDate;


               SPWeb createWebsite;
               SPWeb _w = properties.Web;
               SPWebTemplate spWebTemplate = _w.Site.GetWebTemplates(1030)["STS#1"];
               string title = properties.AfterProperties["Title"].ToString();
               createWebsite = _w.Webs.Add(title, title, "Sagwebsted omkring " + title, 1030, SPWebTemplate.WebTemplateSTS, false, false);
               // SPContentType obsolete = _w.ContentTypes[SPContentTypeId("0x01")];



       }


    }
}

------------EDIT 2 -----------

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EventReceiverProject1.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);


       }

       /// <summary>
       /// An item was added
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);

           DateTime dt = DateTime.Now;
           String strDate = "";
           strDate = dt.ToString("HH.mm.ss");

           properties.ListItem["Title"] = "Kevi2n Changed by event receiver at" + strDate;
           properties.ListItem.Update();
           //properties.AfterProperties["Title"] = "Kevi2n Changed by event receiver at" + strDate;


           SPWeb createWebsite;
           SPWeb _w = properties.Web;
           SPWebTemplate spWebTemplate = _w.Site.GetWebTemplates(1030)["STS#1"];
           string title = properties.ListItem["Title"].ToString();
           createWebsite = _w.Webs.Add(title, title, "Sagwebsted omkring " + title, 1030, SPWebTemplate.WebTemplateSTS, false, false);
           // SPContentType obsolete = _w.ContentTypes[SPContentTypeId("0x01")];

       }


    }
}

A link to the Visual Studio File. https://docs.google.com/file/d/0B9A1BCW2haAxaU03bUdtRVpScnM/edit?pli=1

share|improve this question
    
What is the error? Which line is causing the error? Does your code compile in sandboxed solution? –  Steve B Jul 26 '12 at 8:54
    
There is no error on my local sharepoint server, when i debug it. I compile it to a .wsp, upload it to Sharepoint online, activate it, activate the feature. Nothing happens. –  Kevin Simper Jul 26 '12 at 9:30
    
Then check your even handler registration. The company I work for have an Office365 based solution doing almost exactly what you're trying to do - and it is possible. Though you may find you're limited by the 30sec processing time. And I'd suggest using ItemAdded rather than Adding. –  Andy Burns Jul 26 '12 at 10:59
    
Could you provide some code on how to do that? I have not been able to create a SPWeb on Sharepoint Online, but over 100 on my testserver. I have updated the code to work with ItemAdded, but still no luck. –  Kevin Simper Jul 26 '12 at 12:38
    
Please do not post the same, or very similar question twice. I am merging this with your other question. –  SPDoctor Jul 26 '12 at 15:51

3 Answers 3

up vote 2 down vote accepted

Try this:

SPWeb createWebsite;
SPWeb _w = properties.Web;
SPWebTemplate spWebTemplate = _w.Site.GetWebTemplates(1030)["STS#1"];
string title = properties.ListItem["Title"].ToString();

_w.AllowUnsafeUpdates = true;           
createWebsite = _w.Webs.Add(title, title, "Sagwebsted omkring " + title, 1030, SPWebTemplate.WebTemplateSTS, false, false);
_w.update();
_w.AllowUnsafeUpdates = false;
share|improve this answer
    
Thanks, it was exactly what I was looking for! But i was a damn bug which prevented me from creating sites. What a bummer, but really thanks for your reply! –  Kevin Simper Jul 31 '12 at 8:23

This is likely due to lack of permissions. In SharePoint-online we also can't use RunWithElevatedPrivileges, which executes the specified method with Full Control rights even if the user does not otherwise have Full Control.

However, you best chance is to develop a custom full trust proxy to complete this task.

See how to write a full trust proxy

share|improve this answer
    
Hi @Falak. It happends to be a bug in the Sharepoint Online platform. Thanks for your reply, i really appreciate it! –  Kevin Simper Jul 31 '12 at 8:22

I saw your question earlier but could not get a chance to reply. In your code you had done something like this:

SPWeb web = ParentWeb.Webs.Add("param1","param2",...);

However, you had not allowed unsafe updates to the Web under which the sites were being created. Do it like this:

ParentWeb.AllowUnsafeUpdates = true;
SPWeb web = ParentWeb.Webs.Add("param1","param2",...);
ParentWeb.Update()
ParentWeb.AllowUnsafeUpdates = false;

I have previously created sites programmatically in SharePoint Online using thi same approach.

share|improve this answer
    
Ah found your other question. I have posted my answer there too. –  Vardhaman Deshpande Jul 26 '12 at 15:44
    
Thanks for your reply, I tried it, but i then contacted the Sharepoint Online Support and there was a bug on that account, which prevented me from creating Sites. What a bummer! But super thanks for your reply! –  Kevin Simper Jul 31 '12 at 8:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.