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

So heres a conceptual question that I can't really find in any books. Is it possible to programmatically write to 2 different lists a the same time. I mean I think it is, but my code is not working which means it's probably a syntax error, if it's possible.

share|improve this question
    
Provide code, and what do you mean at one time? –  thantos Nov 9 '12 at 23:41
    
What thantos is getting at is do you mean literally in parallel via multiple threads or would a foreach operation writing the same item to each list one immediately after the other be acceptable? –  Robert Kaucher Nov 10 '12 at 2:49
    
Will, could you edit this question to more accurately reflect the intent? It seems a lot of us thought you meant "writing to 2 different lists" to mean writing a list item to two different lists. –  Robert Kaucher Nov 11 '12 at 1:59

2 Answers 2

up vote 1 down vote accepted

By the description of your question I guess your talking about creating multiple lists using a single feature, if that's right then yes you can have multiple list definitions associated to a single feature but I wouldn't going to recommend this as it can lead to development issues in future.

but still if you want to go it then here's a simple example,

Your Feature file will look like this,

<Feature 
Title="Feature Multiple Lists" 
Description="Feature with three lists." 
Id="put a GUID in here" 
Scope="Site" 
Version="1.0.0.0" 
Hidden="TRUE" 
xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elementfileA.xml" />
    <ElementManifest Location="elementfileB.xml" />
    <ElementManifest Location="elementfileC.xml" />
  </ElementManifests>
</Feature>

In above feature code I declared three elementmanifest with location to a element file, element.xml contains a definition for a feature element.

Now you can declare a elementfileA with a tag for ListTemplate,

<?xml version="1.0" encoding="utf-8"?>
<Elements Id="anotherGUID" xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListTemplate 
  Name="elementfileA" 
  DisplayName="DisplayName;" 
  Description="Description;" 
  BaseType="0" 
  Type="11001" 
  OnQuickLaunch="FALSE" 
  SecurityBits="11" />
</Elements>

you can create two more element files with respected names as you declared them in feature.xml, for the above listTemplate, make sure you create a new folder with same name as listtemplate e.g. in this case you will have three folders named as elementfileB, elementfileA or elementfileC with each list's schema.xml in them.

For more information on this you can look at this MSDN article

However if you want to add multiple lists using code then you can try something like this,

using (SPSite oSPsite = new SPSite("http://Web URL"))
{
    oSPsite.AllowUnsafeUpdates = true;

    using (SPWeb oSPWeb = oSPsite.OpenWeb())
    {
        oSPWeb.AllowUnsafeUpdates = true;

        /* 1. create list from custom ListTemplate present within ListTemplateGalery */
        SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);
        SPListTemplate template = lstTemp["custom template name"];
        oSPWeb.Lists.Add("List Name 1", "Description", template);


        /* 2. create list from sharepoint list content type (e.g. Links) */
        oSPWeb.Lists.Add("List Name 2", "Description", SPListTemplateType.Links);

        oSPWeb.AllowUnsafeUpdates = false;
    }

    oSPsite.AllowUnsafeUpdates = false;
}

For more information on creating a list using check this Link out

I am no master so if you see any mistake let me know,

hope it helps :)

share|improve this answer
    
THANKSFOR THE HELP! –  will Nov 25 '12 at 0:34
    
No problem at all :) –  Muhammad Raja Nov 25 '12 at 8:17

Please provide more information about effect you want to achieve, because I'm not sure if I understand you correctly.

Theoretically, it should be possible to write / update any number of lists at the same time, using for example parallel threads. You can simplify this as - threads as users. So, when 2 users can edit 2 different lists at the same time, why wouldn't it work on 2 threads?

Just make sure you called Update() method on list / web / or any object you updated. This method is critical, do not forget about it. @TimeToThine example is good, but is missing this information.

BTW. You can have as many as list you want in one feature.

share|improve this answer

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.